Fragments (ViewPager) 中 Recyclerviews 之间的通信
Communicate between Recyclerviews in Fragments (ViewPager)
我有 3 个片段,我通过水平滑动 (ViewPager) 访问它们。在每个 Fragment 中都有一个 recyclerview(来源、翻译和描述)。现在我已经实现了代码以启用在 recyclerview 中选择项目。我现在喜欢的是,当我单击第一个 recylcerview 中的一个项目时,第二个和第三个 recylcerview 中的一个项目也会被选中。
我是否需要查看启动片段的 activity ,或者推荐的方法是什么。我不需要代码,只需要最好的方法...
非常感谢
我会使用 activity 和回调来与其余片段通信 -> 这样做你可以将所有内容放在一个地方,而且我认为代码更容易维护。
例如,您有 FragmentA、FragmentB、FragmentC。
您需要像这样创建侦听器:
public interface OnRecyclerviewListener{
public void onItemClick(View v); //or something like for need in another fragments
}
在您的 Activity 中,您需要设置此侦听器以与其他片段进行通信
public class BaseActivity extand Activity{
private OnRecyclerviewListener onRecyclerviewListener=null;
public OnRecyclerviewListener getRecyclerviewListener(){
return onRecyclerviewListener;
}
public void setRecyclerviewListener(OnRecyclerviewListener listener){
this.onRecyclerviewListener=listener;
}
Fragmetn B 和 C 必须实现此 Listener 并在 onCreate(...)
中设置为 BaseActivity
((BaseActivity)getActivity()).setRecyclerviewListener(this);
在 Fragment A 中可以调用
if(((BaseActivity)getActivity).getRecyclerviewListener()!=null){
((BaseActivity)getActivity).getRecyclerviewListener().onItemClick(view);//or something what you set in interface method;
}
您有多种方法可以实现此目的,但总的来说,您正在寻找 publish/subscriber/observer 解决方案。
实现此目的的一种方法是我利用托管 Activity 到 communicate among fragments. In this approach, you define an interface and have your Fragments each implement that interface, then when one of the RecyclerViews' items is selected, you notify the other Fragments. Another solution is to use an EventBus like GreenRobot's EventBus or Otto。
我个人喜欢 EventBus 方法,因为它清晰、简单并且需要的代码更少。
我有 3 个片段,我通过水平滑动 (ViewPager) 访问它们。在每个 Fragment 中都有一个 recyclerview(来源、翻译和描述)。现在我已经实现了代码以启用在 recyclerview 中选择项目。我现在喜欢的是,当我单击第一个 recylcerview 中的一个项目时,第二个和第三个 recylcerview 中的一个项目也会被选中。 我是否需要查看启动片段的 activity ,或者推荐的方法是什么。我不需要代码,只需要最好的方法...
非常感谢
我会使用 activity 和回调来与其余片段通信 -> 这样做你可以将所有内容放在一个地方,而且我认为代码更容易维护。
例如,您有 FragmentA、FragmentB、FragmentC。 您需要像这样创建侦听器:
public interface OnRecyclerviewListener{
public void onItemClick(View v); //or something like for need in another fragments
}
在您的 Activity 中,您需要设置此侦听器以与其他片段进行通信
public class BaseActivity extand Activity{
private OnRecyclerviewListener onRecyclerviewListener=null;
public OnRecyclerviewListener getRecyclerviewListener(){
return onRecyclerviewListener;
}
public void setRecyclerviewListener(OnRecyclerviewListener listener){
this.onRecyclerviewListener=listener;
}
Fragmetn B 和 C 必须实现此 Listener 并在 onCreate(...)
中设置为 BaseActivity((BaseActivity)getActivity()).setRecyclerviewListener(this);
在 Fragment A 中可以调用
if(((BaseActivity)getActivity).getRecyclerviewListener()!=null){
((BaseActivity)getActivity).getRecyclerviewListener().onItemClick(view);//or something what you set in interface method;
}
您有多种方法可以实现此目的,但总的来说,您正在寻找 publish/subscriber/observer 解决方案。
实现此目的的一种方法是我利用托管 Activity 到 communicate among fragments. In this approach, you define an interface and have your Fragments each implement that interface, then when one of the RecyclerViews' items is selected, you notify the other Fragments. Another solution is to use an EventBus like GreenRobot's EventBus or Otto。
我个人喜欢 EventBus 方法,因为它清晰、简单并且需要的代码更少。