将值传递给选项卡片段
问题描述:
我在FragmentTabHost
中有两个选项卡 - 我的问题是如何将数据传递到选定片段?将值传递给选项卡片段
这里是我的代码:
mTabHost.addTab(mTabHost.newTabSpec("clips").setIndicator(("Clips")),
MyClipsFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("clipboards").setIndicator("Clipboards"),
FragmentSearchMyClipboards.class, null);
答
读取(3ED参数)
Bundle myBundle = new Bundle()
myBundle.putInt("paramKey", 1);
mTabHost.addTab(mTabHost.newTabSpec("clips").setIndicator(("Clips")),
MyClipsFragment.class, myBundle);
+0
它抱怨最后一个参数是捆绑? – user1007522 2016-03-03 13:05:22
答
其实这个问题归结两个片段之间进行通信。唯一的办法是通过持有fragment
的活动。这可以通过在这两个片段中声明一个接口并在活动中实现它们来完成。尝试在该束在这里http://developer.android.com/training/basics/fragments/communicating.html
答
通过片段进行通信的最简单的方法是使用EventBus - https://github.com/greenrobot/EventBus
方法文档: 1.这些行添加到片段,其需要获得的信息:
@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));
-
制作类对象的你要发布:
public class EventBusMsg {
//一些信息... }
重复http://stackoverflow.com/questions/23467612/communication-between-fragments-of-fragment-tab-host – Lukas 2014-11-05 17:22:09