ViewPager nested in ViewPager

后端 未结 3 1032
离开以前
离开以前 2020-12-25 09:47

I\'m really newbie in android and I would appreciate any help for my course work.

I need to do:

1) two ViewPagers (not nested) in one Activity

2) two

相关标签:
3条回答
  • 2020-12-25 10:33

    A solution about having a nested Viewpager and disabling the nested Viewpager swiping (if you want to set the selected pages with buttons in the nested)

    For the nested You SubClass ViewPager and you add these:

     public boolean onInterceptTouchEvent(MotionEvent event) {
            //if you want to send the Touches to this Viewpager (the nested) //change true to false
          return true;    
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // Never allow swiping to switch between pages on this inner 
    
            //send swipe to father instead of child
            if (this.getParent()!=null &&  this.getParent() instanceof ViewPager) {
                ((ViewPager) this.getParent()).onTouchEvent(event);
            }
            return false;
        }
    

    In the Layouts you put this:

    <com.mypackage.mycustomapp.myviews.MyViewPager
                android:id="@+id/myNestedviewpager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
    0 讨论(0)
  • 2020-12-25 10:38

    I think you should reconsider having a viewpager inside another viewpager. You will have a lot of problems with the touch events plus the user experience might be confusing/tricky, I suggest you to rethink that one.

    Now for question 1:

    Declare both viewpagers in the xml file of the activity (/layout/activity.xml), for example:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <android.support.v4.view.ViewPager
                android:id="@+id/viewpager1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
        <android.support.v4.view.ViewPager
                android:id="@+id/viewpager2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    Then inflate the xml on the onCreate method of your activity and wire both of the viewpagers:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
    
        mViewpager1 = (ViewPager) findViewById(R.id.viewpager1);
        mViewpager2 = (ViewPager) findViewById(R.id.viewpager2);
    }
    

    Don't forget to declare those two variables private ViewPager mViewpager1, mViewpager2;

    Then you can do whatever you want with mViewpager1 and mViewpager2. One more tip, i suggest you to use adapters to set the pages instead of adding them manually one by one to each viewpager, it will be much cleaner and better to operate with.

    0 讨论(0)
  • 2020-12-25 10:41

    I added an OnTouchListener to the interior ViewPager:

    private OnTouchListener mSuppressInterceptListener = new OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(
                    event.getAction() == MotionEvent.ACTION_DOWN &&
                    v instanceof ViewGroup
            ) {
                    ((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
            }
            return false;
        }
    };
    

    This just detects ACTION_DOWN touch events on the inner ViewPager and prevents the outer one from intercepting it. Because it returns false, only the ACTION_DOWN event should be hit; all the other events will be ignored. You can add this listener to every element you want to "protect" from the outer ViewPager's scrolling, though obviously if you want to pick up any other touch behaviour on those elements you'll need to deal with them inside the touch listener and possibly implement a better listener.

    0 讨论(0)
提交回复
热议问题