Search code examples
androidxmlviewopengl-esandroid-viewpager

ViewPager pageSelected is not working properly


I'm trying to add a ViewPager to my app, and it is not working really good. This is the case:

  • I start the application, in position 0 of my Viewpager, everything is fine.
  • I scroll right to position 2 and all still working
  • I scroll to position 3 and it is empty (sometimes)
  • After this, if I back to position 0 or 3 it is empty always, just position 1 is working

I have been a lot of days thinking that it was my code (I'm working with an awesome openGL app which is using states for change between pages, and I'm trying to adapt this states to a ViewPager). But today I tried to change the position of my layouts, and then I saw that again the one which is not working was the middle one (which was the old position 0). So I really think that the problem is about how I created my Viewpager.

Here is my code:

 <RelativeLayout
    android:id="@+id/myApp_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <com.android.myApp3d.ui.GLRootView
                android:id="@+id/gl_root_view1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <com.android.myApp3d.ui.GLRootView
                android:id="@+id/gl_root_view2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>


            <com.android.myApp3d.ui.GLRootView
                android:id="@+id/gl_root_view3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>


        </android.support.v4.view.ViewPager>
        <View android:id="@+id/gl_root_cover_myApp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fafafa"
            />
    </LinearLayout>
</RelativeLayout>

This is my CustomAdapter for the ViewPager:

public class CustomPagerAdapter extends PagerAdapter {

private Context mContext;
private static final String TAG = "CustomPageAdapter";
public CustomPagerAdapter(Context context) {
    mContext = context;
}

@Override
public Object instantiateItem(ViewGroup collection, int position) {
    int resId = 0;
    switch (position) {
        case 0:
            resId = R.id.gl_root_view1;
            break;
        case 1:
            resId = R.id.gl_root_view2;
            break;
        case 2:
            resId = R.id.gl_root_view3;
            break;
    }
    return collection.findViewById(resId);
}

@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
    collection.removeView((View) view);
}

@Override
public int getCount() {
    return 3;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

    @Override
    public CharSequence getPageTitle(int position) {
return "something";}}

And here is the code of my MainActivity where I initialize my ViewPager:

 public void initView() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setActionBar(mToolbar);
    setToolbar(mToolbar);

    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    mViewPager.setAdapter(new CustomPagerAdapter(this));
    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            setGLRootView(position);
            getGLRoot().lockRenderThread();
            showScreen(position);
            getGLRoot().unlockRenderThread();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });

}

public void showScreen(int position) {
    if (position > 2) {
        position = 1;
    }
    switch (position) {

    case 0:
        startMusicPage(); 
        break;
    case 1:
        startImagePage(); 
        break;
    case 2:
        startVideoPage(); 
        break;
    default:
        break;
    }
    mToolbar.setTitle(getResources().getStringArray(
            R.array.title_array_nav_items)[position]);

    mToolbar.setNavigationContentDescription("drawer");
}

Solution

  • I don't know why it is happening, but if the first time you see them right but not the following times, you cant try:

    viewPager.setOffscreenPageLimit(2);
    

    This will load all the pages only once, so you have to evaluate whether it is important or not to load all of them when loading.