夹住Tab时如何更新碎片数据 [英] how to update fragement data when swiching Tabs
问题描述
我正在使用材料"选项卡,而我使用了3个选项卡.
I am using Material Tab that I used 3 tabs.
标签A [产品列表]
Tab A [ Product List ]
具有回收列表和从远程服务器填充的数据.例如,它具有商品标题,图片和收藏夹图片
Has a Recycle list and data populating from remote Server. For ex, it has Product title, image and favorite image
标签B [收藏的产品列表]
Tab B [ Favorite Product List ]
Tab C [联系我们]
Tab C [ Contact Us ]
我尝试实现的功能,一旦用户选择收藏夹"按钮(添加到收藏夹),该特定列表项就会显示在TAB_B [收藏的产品列表]中
What I try to implement, Once user select on favorite button ( add to favorite ) that particular list item show in TAB_B [ Favorite Product List ]
我使用了共享首选项,并将数据保存为Gson格式.我已经按照本教程进行学习
I have used shared preference and save data as Gson format. I have followed this tutorial
http://androidopentutorials.com/android如何在共享首选项中存储值列表
问题是,一旦我添加项目并从产品列表" [选项卡A]中删除项目,效果就很好,并且没有在选项卡B [收藏的产品列表]中显示.重新启动应用程序后,选项卡b中将显示收藏夹产品列表".我使用了一些技巧,例如广播接收器之类的东西,但是它不能正常工作.
The issue is, once I add Item and remove item from Product List [ Tab A ] perform well, and it is not showing at Tab B [ Favorite Product List ]. Once I restart the app Favorite Product List is showing at Tab b. I used some tricks like broadcast receivers kind of things but it is not working correctly.
我使用 it.neokree:MaterialTabs:0.11
库填充标签页
这是我使用过的一些代码.
Here is my some codes that I have been used.
请指导我,我做错了地方.
Please guide me, where I did wrong.
推荐答案
- 在gradle文件中添加
compile'org.greenrobot:eventbus:3.0.0
' -
在每个片段中添加onCreate()
- add
compile 'org.greenrobot:eventbus:3.0.0
' in gradle file In every fragment add in onCreate()
EventBus.getDefault().register(this);
在每个片段中添加onDestroyView
In every fragment add in onDestroyView
EventBus.getDefault().unregister(this);
为事件创建Java类示例:
Create Java class for Event Example:
public class EventUpdateList {
private boolean listNeedUpdate= true;
public EventUpdateList (boolean listNeedUpdate) {
this.listNeedUpdate= listNeedUpdate;
}
public boolean isListNeedUpdate() {
return listNeedUpdate;
}
}
在所有片段中添加
In all fragments add
@Subscribe(threadMode = Main)
public void onListRefresh(EventUpdatelist eventUpdateList)
{
if (eventUpdateList.isNeedListUpdate)
{
// refresh your list
}
}
在您的活动中添加
In your activity add
EventBus.getDefault.post(new EventUpdateList(true));
所有3个片段都获取有关事件的信息以刷新列表.您甚至可以发布带有完整列表的Event来刷新(而不是此布尔值),并分成3个片段.
And all 3 fragments getting information about event to refresh list. You can even post Event with full list to refresh instead of this boolean and get in 3 fragments.
这篇关于夹住Tab时如何更新碎片数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!