与TabLayout同一片段的不同实例之间的通信 [英] Communication between different instances of same fragment with TabLayout

查看:229
本文介绍了与TabLayout同一片段的不同实例之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用TabLayout与ViewPager展现在我的应用程序标签。我只有两个选项卡。这两个选项卡使用相同的片段(不同的实例),我在每个片断有一条回收。每回收显示不同的数据。

在我的 FragmentPagerAdapter ,我这样做:

  @覆盖
    公共片段的getItem(INT位置){
        束束=新包();
        如果(位置== 0){
            bundle.putString(TreinamentosFragment.EXTRA_TIPO,TreinamentosFragment.TIPO_PENDENTE);
        }其他{
            bundle.putString(TreinamentosFragment.EXTRA_TIPO,TreinamentosFragment.TIPO_CONCLUIDO);
        }        片段片段=新TreinamentosFragment();
        fragment.setArguments(包);
        返回片段;
    }

通过这种方法我能知道在运行时是什么类型的每个片段
并获得Web服务的正确信息来显示。

不过,问题是,当用户在片段A并以某种方式与回收项目之一互动,我删除从适配器这个项目,我需要把这个项目中的片段B的适配器哪有我做到这一点?如何恢复片段B的具体实例,并改变它的适配器?

我想这样做没有用毫不客气的事件总线的。

我的片段A和B是已经在另一个片段里,因为我使用的是抽屉式导航了。


解决方案

 类TreinamentosFragment扩展片段{
   公共onItemClick(项目项目){      清单碎片= getParentFragment()getChildFragmentManager()getFragments()。
      对于(片段片段:片段){
          如果(片段的instanceof TreinamentosFragment&放大器;&放大器; TreinamentosFragment.TIPO_CONCLUIDO .equalIgnoreCase(fragment.getArgments()的getString(TreinamentosFragment.EXTRA_TIPO))。){
            (TreinamentosFragment)fragment.addItem(项目);
         }
      }
      //要么
      清单碎裂= getParentFragment()getChildFragmentManager()FindFragmentByTag(机器人:切换:+ R.id.viewpager +1)。;
      如果(碎裂的instanceof TreinamentosFragment){
         (TreinamentosFragment)fragmentA.addItem(项目);
      }   }   公众的addItem(项目项目){   }
}

I'm using TabLayout with ViewPager to show tabs in my app. I have only two tabs. These two tabs uses the same fragment (different instances) and I have one recycler in each fragment. Every recycler shows different data.

In my FragmentPagerAdapter, I do this:

 @Override
    public Fragment getItem(int position) {
        Bundle bundle = new Bundle();
        if (position == 0) {
            bundle.putString(TreinamentosFragment.EXTRA_TIPO, TreinamentosFragment.TIPO_PENDENTE);
        } else {
            bundle.putString(TreinamentosFragment.EXTRA_TIPO, TreinamentosFragment.TIPO_CONCLUIDO);
        }

        Fragment fragment = new TreinamentosFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

With that approach I'm able to know at runtime what type is every fragment and Get the correct informations in web service to show.

But, the problem is, when user is in Fragment A and interact in some way with one of the recycler items, I remove this item from the adapter and I need to put this item in the adapter of Fragment B. How can I do this? How can I recover the specific instance of Fragment B and change its adapter?

I wanna to do this without use of no kind of event bus.

My Fragment A and B are already inside another fragment because I'm using Navigation Drawer too.

解决方案

class TreinamentosFragment extends Fragment{


   public onItemClick(Item item){

      List fragments =  getParentFragment().getChildFragmentManager().getFragments();
      for(Fragment fragment : fragments ){
          if(fragment  instanceof TreinamentosFragment  &&TreinamentosFragment.TIPO_CONCLUIDO .equalIgnoreCase(fragment.getArgments().getString(TreinamentosFragment.EXTRA_TIPO))){
            (TreinamentosFragment )fragment.addItem(item);
         }
      }
      //or
      List fragmentA =  getParentFragment().getChildFragmentManager().FindFragmentByTag("android:switcher:" + R.id.viewpager + ":1");
      if(fragmentA instanceof TreinamentosFragment){
         (TreinamentosFragment )fragmentA.addItem(item);
      }

   }

   public addItem(Item item){

   }
}

这篇关于与TabLayout同一片段的不同实例之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆