0

I have a ViewPager2 with three pages, two of which hold RecyclerViews. This ViewPager2 is held within a Fragment. When I set a click listener on the view pager before adding RecyclerViews, it worked, but afterwards neither of the two pages will respond to clicks. The Third page that is responding to clicks (using a click listener on the ViewPager) is still responding slowly. The listener is used to open up a bottom sheet.

Here is a section of the Fragment layout containing the ViewPager:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="6dp"
    app:cardElevation="0dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:fontFamily="@font/sf_pro_display_semibold"
            android:textColor="@color/black3"
            android:includeFontPadding="false"
            android:layout_marginStart="20dp"
            android:layout_marginTop="12dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/vector"
            android:layout_marginStart="2dp"
            app:layout_constraintTop_toTopOf="@id/count"
            app:layout_constraintStart_toEndOf="@id/count"
            app:layout_constraintBottom_toBottomOf="@id/count" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/MORE"
            style="@style/TopBarButton"
            android:textColor="@color/blue"
            android:includeFontPadding="false"
            android:layout_marginEnd="20dp"
            app:layout_constraintTop_toTopOf="@id/icon"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="@id/icon"
            />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/horizontal_guideline_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_begin="49dp" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:background="@drawable/custom_background"
            app:tabMode="fixed"
            app:tabGravity="fill"
            app:tabSelectedTextColor="@color/blue"
            app:tabTextColor="@color/gray"
            app:tabIndicatorAnimationMode="linear"
            app:tabIndicator="@drawable/custom_tab_indicator"
            app:tabIndicatorHeight="42dp"
            app:tabIndicatorColor="@color/Transparent"
            app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
            app:tabRippleColor="@null"
            android:layout_marginHorizontal="20dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/horizontal_guideline_one"
            app:layout_constraintEnd_toEndOf="parent" />

        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/blurLayer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tab_layout"
            app:layout_constraintEnd_toEndOf="parent" >

            <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="74dp"
                android:layout_marginHorizontal="20dp" />
        </androidx.appcompat.widget.LinearLayoutCompat>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

And one of the view pager Fragments (The both use the same layout file):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    tools:background="@color/white">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        app:spanCount="3"
        android:clipToPadding="false"
        android:paddingTop="@dimen/margin_12"
        android:visibility="gone"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:listitem="@layout/list_item" />



<!-- This is the recycler view I'm having issues with ->

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_that_wont_register_clicks"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:orientation="horizontal"
        android:clipToPadding="false"
        android:paddingTop="@dimen/margin_12"
        android:clickable="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:listitem="@layout/list_item" />
</androidx.constraintlayout.widget.ConstraintLayout>

The code inthe Fragment that sets the ClickListener:

 binding.container.viewPager.run {
     adapter = fragmentAdapter
     isUserInputEnabled = false
     setOnClickListener {
         MyBottomSheet().show(parentFragmentManager, MixModeBottomSheet.TAG)
     }
 }

I think it's because the RecyclerView items are in front of the RecyclerView itself (from using the Layout Inspector), how do I bring the RecyclerView itself into the foreground so that it responds to clicks and not the list items?

I tried setting click listeners on the RecyclerView within each of the ViewPager's fragments, which did not work and I set click listeners on each itemView for the RecyclerView, which was extremely slow and pretty much useless. I have tried defining a public function in the Activity and tried calling it from the Fragment within the ViewPager, but it would still not respond to clicks.

I've also implemented this, but it only responds to clicks to empty space in the RecyclerView: https://stackoverflow.com/a/51197985/19771695

Any help is much appreciated, thanks.

1 Answers1

0

So, I found a solution: I ended up combining two incomplete solutions together and I think it works well. First, I used the custom RecyclerView OnTouchListener that I linked above to respond to clicks to empty space in the RecyclerView. Second, when I first set click listeners to each RecyclerView item in onBindViewHolder, I was using a callback that opened the BottomSheet from the parent Fragment and was very slow. Instead I used the function I defined earlier in the Activity to open the BottomSheet without any noticeable lag.

Update: It still seems a little slow, so I'm still open to alternative solutions, thank you!