0

I have a ViewPager with 6 screens (mViewPagerMain). At screen 4 I have another ViewPager inside the other (mViewPagerSub). I disable scrolling for the mViewPagerMain at my CustumViewPager, when mViewPagerSub is used - works great. Now the problem: When I scroll through the main pages everything works fine, all things load. When I scroll back from screen 4 (containing the sub) to screen 2 and back to 4, the first 2 screens inside the sub aren't loaded. If I scroll forth and back in the sub, everything loads correctly.

onCreate:

protected void onCreate(Bundle savedInstanceState) {
   mSectionsPagerAdapterMain = new SectionsPagerAdapterMain(getSupportFragmentManager());
   mSectionsPagerAdapterSub = new SectionsPagerAdapterSub(getSupportFragmentManager());
   mViewPagerMain = (CustomViewPager) findViewById(R.id.pagerMain);
   mViewPagerMain.setAdapter(mSectionsPagerAdapter);
}

SectionPagerAdapters: (just like in the google example, same for the 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;
    }
}

Settingsfragment: (just like in the google example)

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...
    }

}

relevant page: main4():

public static void main4(View rootView) {
    subView = rootView;
    new setAdapterTask().execute();
}

and the async task to load the pager:

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();
}

I tried to use a PageListener like this, to simulate the scrolling, but it had no effect:

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();
        }
}
stefple
  • 1,007
  • 17
  • 28
  • Don't put a swipe gesture detector in swipe gesture detector with the same orientation http://stackoverflow.com/questions/6210895/listview-inside-scrollview-is-not-scrolling-on-android – Yaroslav Mytkalyk Feb 08 '13 at 09:56
  • Please read the question at least, I have no problem with the swiping (maybe I didn't explain it correctly). My Problem is that the second ViewPager doesnt load correctly. I think this is timing problem, but I have no idea where the culprit is. – stefple Feb 08 '13 at 11:39

1 Answers1

1

Answering my own question: This did the trick:

private static class PageListener extends SimpleOnPageChangeListener {
   public void onPageSelected(int position) {
      currentPage = position;
      if(currentPage==4 && mViewPagerSub!=null){
         new setAdapterTask().execute();
      }
   }
}
stefple
  • 1,007
  • 17
  • 28