嵌套ViewPager,调用其setCurrentItem方法失效

今天在一个自定义的ViewGroup中嵌套了ViewPager,为了能显示ViewPager前后两页的内容,但是发现使用过程中,ViewPager的setCurrentItem无效了,始终都是从第0页开始显示,求大侠指导。

自定义ViewGroup代码如下:

public class PagerContainer extends FrameLayout implements ViewPager.OnPageChangeListener { private ViewPager mViewPager; private boolean mNeedsRedraw = false; private Point mCenter = new Point; private Point mInitialTouch = new Point; public PagerContainerContext context { thiscontext, null; } public PagerContainerContext context, AttributeSet attrs { thiscontext, attrs, 0; } public PagerContainerContext context, AttributeSet attrs, int defStyleAttr { supercontext, attrs, defStyleAttr; initData; } private void initData { // 允许子View在其区域内进行绘制 setClipChildrenfalse; // child clip功能在Android3.x之后的版本会因为硬件加速而不起作用,所以这里需要关闭硬件加速功能 setLayerTypeView.LAYER_TYPE_SOFTWARE, null; } @Override protected void onFinishInflate { super.onFinishInflate; try { mViewPager = ViewPager getChildAt0; mViewPager.setOnPageChangeListenerthis; } catch Exception e { throw new IllegalStateException"The root child of PagerContainer must be a ViewPager"; } } @Override protected void onSizeChangedint w, int h, int oldw, int oldh { mCenter.x = w / 2; mCenter.y = h / 2; } @Override public boolean onTouchEventMotionEvent event { switch event.getAction { case MotionEvent.ACTION_DOWN: mInitialTouch.x = int event.getX; mInitialTouch.y = int event.getY; default: event.offsetLocationmCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y; break; } return mViewPager.dispatchTouchEventevent; } @Override public void onPageScrolledint position, float positionOffset, int positionOffsetPixels { if mNeedsRedraw invalidate; } @Override public void onPageSelectedint position { } @Override public void onPageScrollStateChangedint state { mNeedsRedraw = state != ViewPager.SCROLL_STATE_IDLE; } public ViewPager getViewPager { return mViewPager; }
}

布局如下:

 <com.yunos.ui.widget.PagerContainer android:id="@+id/pager_container" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <android.support.v4.view.ViewPager android:id="@+id/viewpager_layout" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center_horizontal"/> </com.yunos.ui.widget.PagerContainer>

MainActivity初始化代码:

 private void initView { mPagerContainer = PagerContainer findViewByIdR.id.pager_container; mViewPager = mPagerContainer.getViewPager; Log.eTAG, "get viewpager current item=" + mViewPager.getCurrentItem; // 如下方法失效,求指导 mViewPager.setCurrentItem1; Log.eTAG, "get viewpager current item=" + mViewPager.getCurrentItem; mViewPager.setOffscreenPageLimitAppPageAdapter.NUM_ITEMS; mViewPager.setClipChildrentrue; mViewPager.setPageMargin15; mFragmentPagerAdapter = new AppPageAdaptergetSupportFragmentManager; mViewPager.setAdaptermFragmentPagerAdapter; }

唉,傻逼了,自己看了下ViewPager关于setCurrentItem的源码,发现:

 if mAdapter == null || mAdapter.getCount <= 0 { setScrollingCacheEnabledfalse; return; }

应该在setAdapter之后再调用setCurrentItem,这种傻逼错误都犯了,对自己无语了。

问题就不删了,给大家也一个警告吧,防止范类似的错误!

mPagerContainer.setCurrentItem1;

这是一行错误代码,container并没有setCurrentItem方法,应该是mViewPager.setCurrentItem1;
setCurrentItem要在setAdapter之后。

mPagerContainer要设置谁的currentIterm。log中打印的确实mViewPager

发表评论

电子邮件地址不会被公开。 必填项已用*标注