我有 4 个标签的 viewpager .. 在每个标签中都有一个片段.我的第一个选项卡是一个带有表单的片段(例如用户)单击保存后,数据将插入数据库中.我的第二个选项卡是另一个带有表单的片段,但它使用第一个选项卡中的数据(我从数据库中获取)来填充微调器.现在,在我从第一个选项卡成功插入数据后,在我的第二个选项卡中,微调器为空.因为我的数据库查询是在第二个片段的 onCreateView() 中实现的,所以它只在应用程序启动时调用一次,所以在 tab 1 和 tab2 之间切换不会启动 onCreateView() 甚至 onResume().对我来说有趣的是,如果我转到 tab4 然后返回到 tab2,我的数据正确地在我的微调器中,所以以某种方式从我当前的选项卡上滑动两个选项卡刷新我的片段.我的问题是,当 onCreateView() 只被调用一次时,我怎样才能正确刷新我的片段?
i got viewpager with 4 tabs .. in each tab there is a fragment. my first tab is a fragment with a form (for example Users) after i click save, the data is inserted in the database. my second tab is another fragment with form but it uses data from the first tab (i am getting it from the database) to populate a spinner. now after i successfully inserted data from my first tab, in my second tab the spinner is empty. since my db query is implemented in onCreateView() in the second fragment, its called only once when the application is started, so changing between tab 1 i tab2 doesn't starts onCreateView() or even onResume(). The interesting thing for me is that if i go to tab4 and then return to tab2, my data is in my spinner properly, so somehow swiping two tabs away from my current tab refreshing my fragment. my question is how can i achieve proper refresh to my fragment when onCreateView() is called only once ?
编辑我试图把 psykhi 建议的方法放在这样的地方:
Edit i tried to put that method psykhi suggested like that:
this.mViewPager.setOffscreenPageLimit(0);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
但这对我不起作用.我错过了什么吗?
but it's not working for me. Am i missing something ?
更新:有更好的方法,请看这里:动态更新 ViewPager?
UPDATE: There is a better way, Please have a look here: Update ViewPager dynamically?
删除此答案的内容,因为这是在 ViewPager
中访问 Fragment
的一种非常糟糕的方法,请参阅上面的链接以获得更好的方法.
Removing content of this answer because it was a really bad way to access Fragment
s inside ViewPager
, please see the above link for better approach.
这篇关于我在 viewpager 选项卡中的片段不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!