0
votes

In my app I have a ConstraintLayout. In this layout I have two main elements:

  • ViewPager
  • RecyclerView

With the ViewPager you can swipe horizontally to change pages. The RecyclerView is vertical. Right now, the RecyclerView consumes all swipe events, including horizontal swipes. I'd love to be able to perform a horizontal swipe (either in the right or left direction) on the RecyclerView and it will dispatch to the ViewPager (i.e. changing pages). Is this possible?

What I tried so far:

  • trying to create a listener on the RecyclerView, problem: since this is a vertical RecyclerView I don't get horizontal swipe events.

Some Code

xml file (shortened):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="my.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ffffffff"
        android:paddingEnd="30dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingStart="30dp">

        <com.gigamole.navigationtabstrip.NavigationTabStrip
            android:id="@+id/nts_center"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_gravity="center"
            app:nts_active_color="#42a4d1"
            app:nts_color="#42a4d1"
            app:nts_corners_radius="1dp"
            app:nts_inactive_color="#ff1a1e23"
            app:nts_size="15sp"
            app:nts_titles="@array/titles"
            app:nts_weight="3dp" />

    </FrameLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="55dp"
        android:scrollbars="vertical" />
</android.support.constraint.ConstraintLayout>

MainActivity

mViewPager = findViewById(R.id.vp);
mNavigationTabStrip = findViewById(R.id.nts_center);

list = new ArrayList<>();

mRecyclerView = findViewById(R.id.recycler_view);

customAdapter = new CustomAdapter(list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLongClickable(false);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setNestedScrollingEnabled(true);
mRecyclerView.setAdapter(customAdapter);
1
You're laying out the RecyclerView on top of your ViewPager, how are they intended to be related to each other?Bryan Dormaier
Thanks for your comment. The RecyclerView is under the ViewPager. That's exactly how they are intended to be positioned: at the very top the ViewPager and right under the RecyclerView (with the rest of the display). Like thisVilib
That's not what your layout shows. Both the RecyclerView and the ViewPager are set with height and width of match_parent and then your NavigationTabStrip is drawn over the same area. The end result is your ViewPager is laid out completely underneath your RecyclerView and FrameLayout containing your NavigationTabStrip. The ViewPager isn't receiving touch info because it is likely by default being consumed by the RecyclerView.Bryan Dormaier
@BryanDormaier Thank you so much for your help! Sorry for the misinterpretation of the layout. But I'm still not able to get it right. I tried 2 things: 1) Not consuming the touch event from the recycleviewer (doesn't work because I don't get the horizontal swipes in the first place) 2) trying to remove both listeners from recyclerview and viewpager and adding a listener to the constraintlayout which delegates the click event from there on (problem: I didn't manage to create such a listener, the constraint layout listener never got called). What is the better approach?Vilib

1 Answers

0
votes

use

         LinearLayoutManager horizontalLayoutManager = new 
         LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

to create your horizontal recyclerView.