[英]View pager different results
I am using view pager with 3 tabs in MainActivity
of my app to show grid images. 我在我的应用程序的
MainActivity
中使用带有3个选项卡的视图寻呼机来显示网格图像。 I have one MovieFragment
for all 3 tabs and I am using FragmentsPagerAdapter
to create new instance of MovieFragment
everytime tab is changed. 我对所有3个选项卡都有一个
MovieFragment
,并且每次更改选项卡时,我都使用FragmentsPagerAdapter
创建MovieFragment
新实例。
//FragmentsPagerAdapter method
@Override
public Fragment getItem(int position) {
switch (position){
case 0: return MoviesFragment.newInstance(buildUrl(Utility.POPULAR_URL));
case 1: return MoviesFragment.newInstance(buildUrl(Utility.RATED_URL));
case 2: return MoviesFragment.newInstance("favorite");
default: return null;
}
}
//Fragment method
public static MoviesFragment newInstance(String uri){
MoviesFragment fragment = new MoviesFragment();
Bundle bundle = new Bundle();
bundle.putString("fragment", uri);
fragment.setArguments(bundle);
return fragment;
}
Now I was getting different results in my app so I debugged this method and I found it calls methods before tab is selected ie when I open app it creates instances for case 0 and case 1
and when I go to second tab
it creates instance for case 2
. 现在我在我的应用程序中得到了不同的结果,所以我调试了此方法,发现它在选择选项卡之前调用了方法,即当我打开应用程序时,它为
case 0 and case 1
创建实例,而当我进入second tab
它为case 2
创建实例。 case 2
。 Now in my second tab when I make changes to database and try to get them in third tab changes are not reflected as it already created instance of third tab
when second tab was selected. 现在,在第二个选项卡中,当我对数据库进行更改并尝试将它们添加到第三个选项卡中时,更改不会反映出来,因为当选择第二个选项
third tab
时,它已经创建了third tab
实例。 How do I solve this problem? 我该如何解决这个问题?
@Sahil This is because in android ViewPager keeps track of previous and next pages only. @Sahil这是因为在android中, ViewPager仅跟踪上一页和下一页。 So when you initialize the viewPager it automatically create first 2 page and call
MoviesFragment
for 1st two tab. 因此,当您初始化viewPager时,它将自动创建前2页,并为第1个2个标签页调用
MoviesFragment
。 And once you start scrolling through, it starts keeping the track of only previous and next page. 一旦开始滚动浏览,它就会开始仅跟踪上一页和下一页。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.