TextView inside RecyclerView not scrolling

I have a TextView that is inside a RecyclerView and those are both inside a fragment. When I try to scroll the Textview it just scrolls the RecyclerView instead.

Fragment Code

<?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="match_parent">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/drink_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="100dp" />


    <Button
        android:id="@+id/rndDrinkBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="25dp"
        android:text="@string/random_drink"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

RecyclerView Item Code

<?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="match_parent"
    android:theme="@style/Theme.BarBuddy.LightText">

   ...

    <TextView
        android:id="@+id/drink_instructions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="20dp"
        android:isScrollContainer="true"
        android:maxHeight="52dp"
        android:scrollbars="vertical"
        android:text="null"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/drink_name" />


   ...

</androidx.constraintlayout.widget.ConstraintLayout>

I want to be able to scroll both the Textview and RecyclerView separately.

>Solution :

Obviously the recyclerView will consume the touch event, so when scrolling the textView the recyclerView will scroll.

You can intercept this by setting a touch listener to your textView inside the ViewHolder:

 textView.setOnTouchListener(object : View.OnTouchListener {
 override fun onTouch(v: View?, event: MotionEvent?): Boolean {
  
  return true
 } 

Leave a Reply