• <legend id='beOXp'><style id='beOXp'><dir id='beOXp'><q id='beOXp'></q></dir></style></legend>

        <bdo id='beOXp'></bdo><ul id='beOXp'></ul>

      <i id='beOXp'><tr id='beOXp'><dt id='beOXp'><q id='beOXp'><span id='beOXp'><b id='beOXp'><form id='beOXp'><ins id='beOXp'></ins><ul id='beOXp'></ul><sub id='beOXp'></sub></form><legend id='beOXp'></legend><bdo id='beOXp'><pre id='beOXp'><center id='beOXp'></center></pre></bdo></b><th id='beOXp'></th></span></q></dt></tr></i><div id='beOXp'><tfoot id='beOXp'></tfoot><dl id='beOXp'><fieldset id='beOXp'></fieldset></dl></div>
    1. <tfoot id='beOXp'></tfoot>

        <small id='beOXp'></small><noframes id='beOXp'>

        使用 OnPageChangeListener 同步两个 ViewPager

        时间:2023-10-04

              • <i id='z55bN'><tr id='z55bN'><dt id='z55bN'><q id='z55bN'><span id='z55bN'><b id='z55bN'><form id='z55bN'><ins id='z55bN'></ins><ul id='z55bN'></ul><sub id='z55bN'></sub></form><legend id='z55bN'></legend><bdo id='z55bN'><pre id='z55bN'><center id='z55bN'></center></pre></bdo></b><th id='z55bN'></th></span></q></dt></tr></i><div id='z55bN'><tfoot id='z55bN'></tfoot><dl id='z55bN'><fieldset id='z55bN'></fieldset></dl></div>
                  <tbody id='z55bN'></tbody>
                <tfoot id='z55bN'></tfoot>
              • <legend id='z55bN'><style id='z55bN'><dir id='z55bN'><q id='z55bN'></q></dir></style></legend>
              • <small id='z55bN'></small><noframes id='z55bN'>

                  <bdo id='z55bN'></bdo><ul id='z55bN'></ul>
                  本文介绍了使用 OnPageChangeListener 同步两个 ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试同步两个 ViewPager,显然我之前有很多人,我已经做到了:

                  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 mNavPager;
                  
                  private ViewPager mMainPager;
                  
                  private final OnPageChangeListener mNavPagerListener = new OnPageChangeListener() {
                  
                      private boolean mNavDragging;
                      private int mScrollPosition;
                  
                      @Override
                      public void onPageSelected(int position) {
                          mScrollPosition = position;
                      }
                  
                      @Override
                      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                          if(mNavDragging)
                              mMainPager.scrollTo(positionOffsetPixels, 0);
                      }
                  
                      @Override
                      public void onPageScrollStateChanged(int state) {
                          switch(state) {
                          case ViewPager.SCROLL_STATE_DRAGGING:
                          case ViewPager.SCROLL_STATE_SETTLING:
                              mNavDragging = true;
                              break;
                          case ViewPager.SCROLL_STATE_IDLE:
                              mNavDragging = false;
                              break;
                          }
                      }
                  };
                  
                  private OnPageChangeListener mMainPagerListener = new OnPageChangeListener() {
                  
                      private boolean mMainDragging;
                      private int mScrollPosition;
                  
                      @Override
                      public void onPageSelected(int position) {
                          mScrollPosition = position;
                      }
                  
                      @Override
                      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                          if(mMainDragging)
                              mNavPager.scrollTo(positionOffsetPixels, 0);
                      }
                  
                      @Override
                      public void onPageScrollStateChanged(int state) {
                          switch(state) {
                          case ViewPager.SCROLL_STATE_DRAGGING:
                          case ViewPager.SCROLL_STATE_SETTLING:
                              mMainDragging = true;
                              break;
                          case ViewPager.SCROLL_STATE_IDLE:
                              mMainDragging = false;
                              break;
                          }
                      }
                  };
                  

                  如果手动滚动其中一个,则另一个使用滚动状态属性从属于它.它可以很好地工作,直到物品到达最终位置;此时,从属寻呼机立即弹回先前选择的项目,就好像没有发生滚动一样.

                  If either one is scrolled manually, the other is slaved to it using the scroll state property. It works beautifully till the items reach their final position; at this point, the slaved pager flicks instantly back to the previously selected item, as though the scrolling hadn't taken place.

                  我曾尝试使用各种不同的逻辑约束调用 ViewPager#setCurrentItem(mScrolledPosition),但这也不起作用,尽管它偶尔会使情况变得更糟.我觉得好像必须有什么可以做的,但我不确定是什么.

                  I have tried calling ViewPager#setCurrentItem(mScrolledPosition) with a variety of different logic constraints but that doesn't work either, though it does occasionally make it worse. I feel as though there must be something that can be done with that but I'm not sure what.

                  我怎样才能让从属寻呼机保持在正确的位置?

                  How can I get the slaved pager to remain in the correct position?

                  推荐答案

                  我使用 OnPageChangeListener 以更简单(更简洁)的方式解决了这个问题:

                  I solved this problem in a much easier (and cleaner) way using the OnPageChangeListener:

                  mViewPager1.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                  
                        private int mScrollState = ViewPager.SCROLL_STATE_IDLE;
                  
                        @Override
                        public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {
                          if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
                            return;
                          }
                          mViewPager2.scrollTo(mViewPager1.getScrollX(), mViewPager2.getScrollY());
                        }
                  
                        @Override
                        public void onPageSelected(final int position) {
                  
                        }
                  
                        @Override
                        public void onPageScrollStateChanged(final int state) {
                          mScrollState = state;
                          if (state == ViewPager.SCROLL_STATE_IDLE) {
                            mViewPager2.setCurrentItem(mViewPager1.getCurrentItem(), false);
                          }
                        }
                  });
                  
                  mViewPager2.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                  
                        private int mScrollState = ViewPager.SCROLL_STATE_IDLE;
                  
                        @Override
                        public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {
                          if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
                            return;
                          }
                          mViewPager1.scrollTo(mViewPager2.getScrollX(), mViewPager1.getScrollY());
                        }
                  
                        @Override
                        public void onPageSelected(final int position) {
                  
                        }
                  
                        @Override
                        public void onPageScrollStateChanged(final int state) {
                          mScrollState = state;
                          if (state == ViewPager.SCROLL_STATE_IDLE) {
                            mViewPager1.setCurrentItem(mViewPager2.getCurrentItem(), false);
                          }
                        }
                  });
                  

                  这篇关于使用 OnPageChangeListener 同步两个 ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将viewpager放在android的片段中? 下一篇:如何在android滑动选项卡布局中对齐活动选项卡中心

                  相关文章

                  <i id='t1WbT'><tr id='t1WbT'><dt id='t1WbT'><q id='t1WbT'><span id='t1WbT'><b id='t1WbT'><form id='t1WbT'><ins id='t1WbT'></ins><ul id='t1WbT'></ul><sub id='t1WbT'></sub></form><legend id='t1WbT'></legend><bdo id='t1WbT'><pre id='t1WbT'><center id='t1WbT'></center></pre></bdo></b><th id='t1WbT'></th></span></q></dt></tr></i><div id='t1WbT'><tfoot id='t1WbT'></tfoot><dl id='t1WbT'><fieldset id='t1WbT'></fieldset></dl></div>
                      <bdo id='t1WbT'></bdo><ul id='t1WbT'></ul>

                    <small id='t1WbT'></small><noframes id='t1WbT'>

                    <tfoot id='t1WbT'></tfoot>
                  1. <legend id='t1WbT'><style id='t1WbT'><dir id='t1WbT'><q id='t1WbT'></q></dir></style></legend>