【问题标题】:Pass values to tab fragment将值传递给选项卡片段
【发布时间】:2014-11-05 17:14:47
【问题描述】:

我在 FragmentTabHost 中有两个标签 - 我的问题是如何将数据传递给选定的片段?

这是我的代码:

mTabHost.addTab(mTabHost.newTabSpec("clips").setIndicator(("Clips")),
                MyClipsFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("clipboards").setIndicator("Clipboards"),
                FragmentSearchMyClipboards.class, null);

【问题讨论】:

标签: android android-fragments android-tabhost android-tabs


【解决方案1】:

在捆绑包中(3ed 参数)

Bundle myBundle = new Bundle()
myBundle.putInt("paramKey", 1);
mTabHost.addTab(mTabHost.newTabSpec("clips").setIndicator(("Clips")),
                MyClipsFragment.class, myBundle);

【讨论】:

  • 它抱怨最后一个参数是一个包?
【解决方案2】:

实际上问题归结为两个片段之间的通信。唯一的方法是通过同时拥有fragment 的活动。这可以通过在两个片段中声明一个接口并在活动中实现它们来完成。尝试阅读这里http://developer.android.com/training/basics/fragments/communicating.html

【讨论】:

    【解决方案3】:

    通过片段进行通信的最简单方法是使用 EventBus - https://github.com/greenrobot/EventBus

    操作方法: 1. 在需要获取信息的fragment中加入这行:

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            EventBus.getDefault().register(this);
            return inflater.inflate(R.layout.event_list, container, false);
    
        }
    
    @Override
        public void onDestroyView() {
            super.onDestroyView();
            EventBus.getDefault().unregister(this);
        }
    public void onEventMainThread(EventBusMsg bus) { //Name your method like this   
            //here you get the info...
        }
    

    2。从任何地方发布信息,如下所示:

    EventBus.getDefault().post(new EventBusMsg(true));
    
    1. 为您要发布的对象创建类:

      public class EventBusMsg {
      

      //一些信息... }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 2018-08-20
      • 2018-01-12
      相关资源
      最近更新 更多