[英]Pass list from Main Activity to tabs Fragment RecyclerView
I have activity which have 4 tabs and each tab has RecyclerView
. 我有4个标签的活动,每个标签都有
RecyclerView
。 I fetch Json data in MainActivity
and then convert it into a List. 我在
MainActivity
获取Json数据,然后将其转换为List。
The challenge here is to pass the list to each RecyclerView
in each Fragment
in each tab. 这里的挑战是将列表传递给每个选项卡中每个
Fragment
中的每个RecyclerView
。
Simplest way would be using ViewModel like this: 最简单的方法是像这样使用ViewModel :
ViewModel: ViewModel:
public class SharedViewModel extends ViewModel {
private List<Item> list_1= ArrayList<Item>();
private List<Item> list_2= ArrayList<Item>();
public void setList_1(List<Item> list) {
list_1 = list
}
public List<Item> getList_1() {
return list_1 ;
}
public void setList_2(List<Item> list) {
list_2 = list
}
public List<Item> getList_2() {
return list_2 ;
}
}
MainActivity: 主要活动:
public class MainActivity extends AppCompatActivity {
SharedViewModel model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_acticity);
model = ViewModelProviders.of(this).get(SharedViewModel.class);
// Getting your lists here
model.setList_1(list_1);
model.setList_2(list_2);
}
}
Fragment_1: 片段_1:
public class Fragment_1 extends Fragment {
private SharedViewModel model;
List<Item> list_1 = new ArrayList<>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
list_1 = model.getList_1();
}
}
Fragment_2: 片段_2:
public class Fragment_2 extends Fragment {
private SharedViewModel model;
List<Item> list_2 = new ArrayList<>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
list_2 = model.getList_2();
}
}
But, better way is more complex. 但是,更好的方法更加复杂。 More info here Guide to App Architecture
这里有更多信息App体系结构指南
You might consider using a BoradcastReceiver
in your Fragment
. 您可以考虑在
Fragment
使用BoradcastReceiver
。 So when the lists for the fragments are updated, you send a broadcast from your MainActivity
and the broadcast will be received in the Fragments. 因此,当片段列表更新时,您从
MainActivity
发送广播,广播将在片段中接收。
You just have to register BroadcastReceiver
in your Fragment
like this. 您只需要像这样在
Fragment
注册BroadcastReceiver
。
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
// You might consider sending a list to be passed to the adapter of the RecyclerView.
String data = intent.getStringExtra("data");
// Update your RecyclerView with data.
}
};
Register the receiver in the Fragment
inside your onResume
function. 在
onResume
函数内的Fragment
注册接收器。
@Override
protected void onResume() {
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "update-frag-1-recyclerview".
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("update-frag-1-recyclerview"));
super.onResume();
}
And un-register the receiver in onPause
function in your Fragment
. 并在
Fragment
onPause
函数中注销接收器。
@Override
protected void onPause() {
// Unregister since the Fragment is paused.
LocalBroadcastManager.getInstance(this).unregisterReceiver(
mMessageReceiver);
super.onPause();
}
Now from your Activity
when the data is received and you are now ready to pass the data to your fragments, just send the broadcasts accordingly to be received by your fragments. 现在,从
Activity
中接收到数据后,就可以将数据传递到片段了,只需发送相应的广播,片段就可以接收这些广播。 I am showing one. 我正在显示一个。
Intent intent = new Intent("update-frag-1-recyclerview");
// You can also include some extra data.
intent.putExtra("data", "This is some data!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
This will trigger the onReceive
function in your Fragment1
as you are listening to the event which is update-frag-1-recyclerview
from Fragment1
. 这将触发
onReceive
功能在你的Fragment1
,你正在听的是事件update-frag-1-recyclerview
从Fragment1
。 You can do the same thing for other fragments as well. 您也可以对其他片段执行相同的操作。 Keep in mind that I am only sending just one data which is
String
. 请记住,我只发送一个
String
数据。 You might consider sending a list as well as an extra along with the intent to be received in the Fragment
. 您可能会考虑发送一个列表以及一个额外的列表,以及在
Fragment
接收的意图。
Hope that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.