PagerAdapter start position

前端 未结 9 1104

I\'m using the following example to impliment my viewPager: http://code.google.com/p/viewpagerexample/issues/list

The problem with this example is that I can\'t figure o

9条回答
  •  醉话见心
    2021-01-30 08:41

    I encountered same problem. When I initialize ViewPager, the indicator position was 0. This may depends on amount of calculating for Pager contents. I use ViewTreeObserver like below.

    mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mViewPager.setCurrentItem(newPosition);
            removeOnGlobalLayoutListener(mSlidingTabLayout.getViewTreeObserver(), mGlobalLayoutListener);
        }
    };
    mSlidingLayout.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);
    

    and,

    private void removeOnGlobalLayoutListener(ViewTreeObserver observer, ViewTreeObserver.OnGlobalLayoutListener listener) {
           if (observer == null) return;
           if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                   observer.removeGlobalOnLayoutListener(listener);
           } else {
                   observer.removeOnGlobalLayoutListener(listener);
           }
    } 
    

    In this way, also never to bother with time setting of delay.

提交回复
热议问题