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);
RecyclerView
on top of yourViewPager
, how are they intended to be related to each other? – Bryan DormaierRecyclerView
and theViewPager
are set with height and width ofmatch_parent
and then yourNavigationTabStrip
is drawn over the same area. The end result is yourViewPager
is laid out completely underneath yourRecyclerView
andFrameLayout
containing yourNavigationTabStrip
. TheViewPager
isn't receiving touch info because it is likely by default being consumed by theRecyclerView
. – Bryan Dormaier