<tfoot id='EVPcW'></tfoot>

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

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

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

      Android 2 ViewPagers 同时滚动

      时间:2023-10-04
      • <i id='SZszL'><tr id='SZszL'><dt id='SZszL'><q id='SZszL'><span id='SZszL'><b id='SZszL'><form id='SZszL'><ins id='SZszL'></ins><ul id='SZszL'></ul><sub id='SZszL'></sub></form><legend id='SZszL'></legend><bdo id='SZszL'><pre id='SZszL'><center id='SZszL'></center></pre></bdo></b><th id='SZszL'></th></span></q></dt></tr></i><div id='SZszL'><tfoot id='SZszL'></tfoot><dl id='SZszL'><fieldset id='SZszL'></fieldset></dl></div>

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

              <tbody id='SZszL'></tbody>
            <legend id='SZszL'><style id='SZszL'><dir id='SZszL'><q id='SZszL'></q></dir></style></legend>
              <bdo id='SZszL'></bdo><ul id='SZszL'></ul>

                <tfoot id='SZszL'></tfoot>
                本文介绍了Android 2 ViewPagers 同时滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                是否有可能有 2 个同时滚动的 ViewPagers,如果我开始滚动其中一个,另一个执行完全相同的滚动行为.或者我应该实现 ViewPager 以外的东西.

                Is it possible to have 2 ViewPagers that simultaniously scroll together, if I start scrolling on one, the other does the exact same scrolling behaviour. Or should I implement somthing other than a ViewPager.

                谢谢

                推荐答案

                最适合我的解决方案是在 OnTouchListener 中的 ViewPager<之间传递 MotionEvent/code> 实例.尝试过假拖动,但它总是很慢而且有问题(也尝试了这个线程的解决方案 - 没用) - 我需要一个平滑的、类似视差的效果.

                The solution that worked best for me was to pass MotionEvent in OnTouchListener between ViewPager instances. Tried fake dragging but it was always laggy and buggy (tried the solution from this thread too - didn't work) - I needed a smooth, parallax-like effect.

                所以,我的建议是实现一个View.OnTouchListener.MotionEvent 必须缩放以补偿宽度差异.

                So, my advice is to implement a View.OnTouchListener. The MotionEvent has to be scaled to compensate for the difference in width.

                public class SyncScrollOnTouchListener implements View.OnTouchListener {
                
                private final View syncedView;
                
                public SyncScrollOnTouchListener(@NonNull View syncedView) {
                    this.syncedView = syncedView;
                }
                
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    MotionEvent syncEvent = MotionEvent.obtain(motionEvent);
                    float width1 = view.getWidth();
                    float width2 = syncedView.getWidth();
                
                    //sync motion of two view pagers by simulating a touch event
                    //offset by its X position, and scaled by width ratio
                    syncEvent.setLocation(syncedView.getX() + motionEvent.getX() * width2 / width1,
                            motionEvent.getY());
                    syncedView.onTouchEvent(syncEvent);
                    return false;
                }
                }
                

                然后将其设置为您的 ViewPager

                    sourcePager.setOnTouchListener(new SyncScrollOnTouchListener(targetPager));
                

                请注意,此解决方案仅在两个寻呼机具有相同方向时才有效.如果您需要它适用于不同的方向 - 调整 syncEvent Y 坐标而不是 X.

                Note that this solution will only work if both pagers have the same orientation. If you need it to work for different orientations - adjust syncEvent Y coordinate instead of X.

                我们还需要考虑一个问题 - 最小的投掷速度和距离,这可能会导致只有一个寻呼机改变页面.

                There is one more issue that we need to take into account - minimum fling speed and distance that can cause just one pager to change page.

                通过向我们的寻呼机添加 OnPageChangeListener 可以轻松解决此问题

                It can be easily fixed by adding an OnPageChangeListener to our pager

                sourcePager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset,
                                                   int positionOffsetPixels) {
                            //no-op
                        }
                
                        @Override
                        public void onPageSelected(int position) {
                            targetPager.setCurrentItem(position, true);
                        }
                
                        @Override
                        public void onPageScrollStateChanged(int state) {
                            //no-op
                        }
                    }); 
                

                这篇关于Android 2 ViewPagers 同时滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                <tfoot id='MHJ5l'></tfoot>

                    • <bdo id='MHJ5l'></bdo><ul id='MHJ5l'></ul>

                        <tbody id='MHJ5l'></tbody>

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

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