我有一个带有6个屏幕的ViewPager(mViewPagerMain)。 在屏幕4,我在另一个内部有另一个ViewPager(mViewPagerSub)。我在CustumViewPager上禁用mViewPagerMain的滚动,当使用mViewPagerSub时 - 效果很好。 现在的问题是: 当我滚动主页时一切正常,所有东西都加载。 当我从屏幕4(包含子屏幕)向后滚动到屏幕2并返回到4时,子视图中的前2个屏幕未加载。如果我向前滚动并返回子,则一切都正确加载。
的onCreate:
protected void onCreate(Bundle savedInstanceState) {
mSectionsPagerAdapterMain = new SectionsPagerAdapterMain(getSupportFragmentManager());
mSectionsPagerAdapterSub = new SectionsPagerAdapterSub(getSupportFragmentManager());
mViewPagerMain = (CustomViewPager) findViewById(R.id.pagerMain);
mViewPagerMain.setAdapter(mSectionsPagerAdapter);
}
SectionPagerAdapters :(就像在google示例中一样,对于sub来说相同)
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new SettingsFragment(0);
break;
case 1:
fragment = new SettingsFragment(1);
break;
case 2:
fragment = new SettingsFragment(2);
break;
// and so on...
default:
fragment = new SettingsFragment(0);
}
return fragment;
}
@Override
public int getCount() {
return 6;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase();
case 1:
return getString(R.string.title_section2).toUpperCase();
// and so on ...
}
return null;
}
}
设置片段:(就像在谷歌示例中一样)
public static class SettingsFragment extends Fragment {
int position = 0;
public SettingsFragment(int position) {
this.position = position;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
if (position == 0) {
rootView = inflater.inflate(R.layout.main0, container, false);
main0(rootView);
return rootView;
} else if (position == 1) {
rootView = inflater.inflate(R.layout.main1, container, false);
main1(rootView);
return rootView;
} else if (position == 2) {
rootView = inflater.inflate(R.layout.main2, container, false);
main2(rootView);
return rootView;
}
//...and so on...
}
}
相关页面:main4():
public static void main4(View rootView) {
subView = rootView;
new setAdapterTask().execute();
}
和加载寻呼机的异步任务:
private static class setAdapterTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result) {
mViewPagerSub = (CustomViewPager)subView.findViewById(R.id.pagerSub);
mViewPagerSub.setAdapter(mSectionsPagerAdapterSub);
mViewPagerSub.refresh();
}
}
CustomViewPager.refresh():
public void refresh() {
getAdapter().notifyDataSetChanged();
}
我尝试使用像这样的PageListener来模拟滚动,但它没有效果:
private static class PageListener extends SimpleOnPageChangeListener {
public void onPageSelected(int position) {
currentPage = position;
if (mViewPagerSub != null && currentPage == 3) {
mViewPagerSub.setCurrentItem(5);
} else if (mViewPagerSub != null && currentPage == 4) {
mViewPagerSub.setCurrentItem(0);
}
if (mViewPagerSub != null)
mViewPagerSub.refresh();
}
}
答案 0 :(得分:1)
回答我自己的问题: 这就是诀窍:
private static class PageListener extends SimpleOnPageChangeListener {
public void onPageSelected(int position) {
currentPage = position;
if(currentPage==4 && mViewPagerSub!=null){
new setAdapterTask().execute();
}
}
}