Synchronizing two ViewPagers using OnPageChangeListener

后端 未结 6 2011
北海茫月
北海茫月 2020-12-08 22:57

I\'m trying to synchronize two ViewPagers, as apparently have quite a lot of people before me, and I\'ve got as far as this:

private ViewPager m         


        
6条回答
  •  囚心锁ツ
    2020-12-08 23:33

    All the Answers are approximately currect in some situation. Here I am giving one more answer which will use to slide both the ViewPagers simultaneously whether there size is same or not:

    viewPagerBanner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        private int scrollState = ViewPager.SCROLL_STATE_IDLE;
        // Indicates that the pager is in an idle, settled state. 
        // The current page is fully in view and no animation is in progress.
    
        @Override
        public void onPageScrolled(int position, float positionOffset, 
                                   int positionOffsetPixels) {
            if (scrollState == ViewPager.SCROLL_STATE_IDLE) {
                return;
            }
            viewPagerTitle.scrollTo(viewPagerBanner.getScrollX()*
                                    viewPagerTitle.getWidth()/
                                    viewPagerBanner.getWidth(), 0);
            // We are not interested in Y axis position
        }
    
        @Override
        public void onPageSelected(int position) {}
    
        @Override
        public void onPageScrollStateChanged(int state) {
            scrollState = state;
            if (state == ViewPager.SCROLL_STATE_IDLE) {
                viewPagerTitle.setCurrentItem(viewPagerBanner.getCurrentItem(), false);
            }
        }
    });
    

提交回复
热议问题