PagerAdapter start position

前端 未结 9 1099

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:36

    I have noticed that if you recreate Activity (orientation change) with ViewPager having FragmentStatePagerAdapter, then the Adapter will reuse it's Fragments. The way to stop it is:

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        if (viewPager != null) {
            // before screen rotation it's better to detach pagerAdapter from the ViewPager, so
            // pagerAdapter can remove all old fragments, so they're not reused after rotation.
            viewPager.setAdapter(null);
        }
        super.onSaveInstanceState(savedInstanceState);
    }
    

    but then after Activity recreation ViewPager alwayes opens page 0 first and setCurrentItem(CurrentPosition); doesn't work. Fix for that is changing page after delay:

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            viewPager.setCurrentItem(newPosition);
        }
    }, 100);
    
    0 讨论(0)
  • 2021-01-30 08:37
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View currentPage = null;
        switch(position){
            case 0:
                currentPage = LayoutInflater.from(context).inflate(R.layout.page0, null)    
                break;
            case 1:
                currentPage = LayoutInflater.from(context).inflate(R.layout.page1, null)    
                ///////////// This page will be default ////////////////////
                ((ViewPager)container).setCurrentItem(position);
                ////////////////////////////////////////////////////////////
                break;
            case 2:
                currentPage = LayoutInflater.from(context).inflate(R.layout.page2, null)    
                break;
        return currentPage;
    }
    
    0 讨论(0)
  • 2021-01-30 08:39

    To start with the last fragment I did this:

    PagerAdapter pagerAdapter = new PagerAdapter();
    viewPager.setAdapter(pagerAdapter);
    viewPager.setCurrentItem(pagerAdapter.getCount() - 1);
    
    0 讨论(0)
  • 2021-01-30 08:40

    I came across a problem whereby if I set the current item before I set the adapter, the first item I get back will always be the one at position 0.

    Make sure you do:

    awesomePager.setAdapter(awesomeAdapter);
    awesomePager.setCurrentItem(CurrentPosition);
    

    and not:

    awesomePager.setCurrentItem(CurrentPosition);
    awesomePager.setAdapter(awesomeAdapter);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-30 08:42

    I have an array of size more than 1000 and in dymanic viewpager I was facing leftswipe stuck on firstload. The below code solved this and resulted in smooth scroll:

    @Override
    onResume(){
       super.onResume();
       viewPager.setCurrentItem(newPosition);
    }
    
    0 讨论(0)
提交回复
热议问题