如果它適合您,請創建List
數據模型的全局實例您要更新RecyclerView
。
List<DataModel> dataModelList = new ArrayList<>();
可以延長Application
類的類,你要做到這一點或其他地方。
現在ActivityA
public class ActivityA extends AppCompatActivity {
YourAdapter adapter;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
adapter = new YourAdapter(YourClassWhereYouPutDataModelList.dataModelList)
recyclerView.setAdapter(adapter);
@Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
}
現在,在您ActivityB
public class ActivityA extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//your code to get value
YourClassWhereYouPutDataModelList.dataModelList.add(YourValue);
//Now it's up to you, either finish it using finish() or continue working
//Whenever you go to ActivityA, RecyclerView will be updated
}
});
}
}
考慮SwipeRefreshLayout? – Akshay
您可以在模型中存儲數據,同時處於活動B中,現在再次回到您的適配器中,您可以使用swiperefreshlayout並根據您的需要實現這些方法:notifyDataSetChanged()或notifyItemChanged(int)或notifyItemRangeChanged(int,int) 。請參閱此鏈接(http://stackoverflow.com/questions/33789345/whats-better-notifydatasetchanged-或-notifyitemchanged-in-loop) –
不,這是行不通的。我希望RW中的新視圖在Activity B中按下按鈕時出現。像Person這樣的東西填充表單字段並提交,然後在活動A –