简体   繁体   English

ViewPager pageSelected 工作不正常

[英]ViewPager pageSelected is not working properly

I'm trying to add a ViewPager to my app, and it is not working really good.我正在尝试将 ViewPager 添加到我的应用程序中,但效果不佳。 This is the case:情况是这样的:

  • I start the application, in position 0 of my Viewpager, everything is fine.我在 Viewpager 的位置 0 启动应用程序,一切正常。
  • I scroll right to position 2 and all still working我向右滚动到位置 2 并且都还在工作
  • I scroll to position 3 and it is empty (sometimes)我滚动到位置 3,它是空的(有时)
  • After this, if I back to position 0 or 3 it is empty always, just position 1 is working在此之后,如果我回到位置 0 或 3 它总是空的,只有位置 1 正在工作

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).我已经有很多天认为这是我的代码(我正在使用一个很棒的 openGL 应用程序,它使用状态在页面之间进行更改,我正在尝试将此状态调整为 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).但是今天我试图改变我的布局的位置,然后我再次看到那个不起作用的是中间的(旧的位置 0)。 So I really think that the problem is about how I created my Viewpager.所以我真的认为问题在于我如何创建我的 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:这是我用于 ViewPager 的 CustomAdapter:

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:这是我初始化 ViewPager 的 MainActivity 的代码:

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

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.这只会加载所有页面一次,因此您必须在加载时评估是否加载所有页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM