3 回答
TA贡献1824条经验 获得超5个赞
使用事件总线
在 Page_1 中注销和注册 EventBusonStop()和onStart()
EventBus.getDefault().unregister(this)
EventBus.getDefault().register(this)
并使用它来发布值
EventBus.getDefault().post(new MessageEvent("Change Color"));
这个函数将处理 MessageEvent
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
//change the color here
//add this function in Page_1
}
当您更新颜色的值时。放入 MessageEvent文档
TA贡献1797条经验 获得超6个赞
当一个 Fragment 可见时(即在你的 ViewPager 中被选中的页面),它的 setUserVisibleHint() 方法被调用。您可以在 Fragment 中覆盖该方法并使用它来触发刷新。
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
//you can check if the color is changed then refresh the fragment if not then don't do anything
//here you should refresh your fragment , this will called every time you
//view this fragment in all cases even if you didn't move to the
//third tab
}
}
如何刷新片段
Fragment currentFragment = getFragmentManager().findFragmentByTag("YourFragmentTag");
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.detach(currentFragment);
fragmentTransaction.attach(currentFragment);
fragmentTransaction.commit();
添加回答
举报