Controlling two ViewPager Together

大憨熊 提交于 2019-11-27 18:28:27

问题


I need to synchronize the two ViewPager together. The requirement is something like on scrolling the ViewPager-1 and the ViewPager-2 should also scroll by certain amount. The Image shown below will make you more clear with my question.

You can also help me with some tutorials link. Thanks.


回答1:


I think this is what you need:

        viewpager1.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                viewpager2.onTouchEvent(event);
                return false;
            }
        });

        viewpager2.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                viewpager1.onTouchEvent(event);
                return false;
            }
        });

I have a same problem, but first I try to use fakeDragBy method, which is dead end. (if you have more than two pages)




回答2:


You can extend ViewPager in order to create a custom view and override onTouchEvent() in the following way:

        @Override
        public boolean onTouchEvent(MotionEvent event) {

          if(mDependentView != null){
            mDependentView.onTouchEvent(event);
          }
          return super.onTouchEvent(event);
        }

Also create a setter inside your custom class in order to set the dependentView

public void setDependentView(View view){
   mDependentView = view;
}

Then you should set the second viewpager as dependent view of the first viewpager in your activity.



来源:https://stackoverflow.com/questions/13757859/controlling-two-viewpager-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!