1

I have a created a custom viewpager, and i would like the user to swipe and change pages only from a part of the screen.

Every page has a linearlayout at the bottom with some text, and i would like to swipe only from there, not from the hole screen.

Is that possible?

This is my custom viewpager class:

public class MyViewPager extends ViewPager {

    private boolean mSwipable = true;

    public MyViewPager(Context context) {
        super(context);
    }

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return mSwipable ? super.onInterceptTouchEvent(event) : false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mSwipable ? super.onTouchEvent(event) : false;
    }

    public boolean isSwipable() {
        return mSwipable;
    }

    public void setSwipable(boolean swipable) {
        mSwipable = swipable;
    }
}

and this is the view of a page:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/fragment_search">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottomLayout"
        android:layout_marginBottom="@dimen/bottom_bar_height">

      //show some stuff here


    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottomMenu"
        android:layout_width="match_parent"
        android:layout_height="@dimen/bottom_bar_height"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

      // this is my footer, where i need to change the pages by swipe
    </LinearLayout>
</RelativeLayout>
menu_on_top
  • 2,613
  • 14
  • 44
  • 71
  • make your viewpager have the same size as your bottom linear layout, so you can scroll only from there, then put a framelayout and change fragments as you swipe the viewpager. I don't think you can handle the swipe event to work only in some views inside de pager. – Jonathan Aste Apr 03 '17 at 17:48

1 Answers1

0

Follow this question in order to be able to disable swiping at will How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

And combine that with your SliderAdapter in order to control which pages will be swipeable or not.

If you followed the link above, on your subclass of ViewPager, you can add some GestureDetector on onTouchEvent(MotionEvent event) and feed it with your event, like Android: How to handle right to left swipe gestures

By doing this, you will now have some onSwipeRight and manage there your logic, delegating to the adapter the question if the page should be swiped or not, or where to go next.

Community
  • 1
  • 1
Darish
  • 11,032
  • 5
  • 50
  • 70