androidandroid-layoutandroid-recyclerview

Scroll Textview inside RecyclerView


I have a RecyclerView

<android.support.v7.widget.RecyclerView
            android:id="@+id/activityRecicler"
            android:layout_width="match_parent"
            android:scrollbars="vertical"
            android:layout_height="wrap_content"
            app:stackFromEnd="true"
            />

And I have a row layout:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/description"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/solutions"/>

    </LinearLayout>

And the wrap content work, but my problem is that TextView with id description It could be very long and so, I would have a Scroll for this TextView. How could I do this?


Solution

  • Change your layout XML to:

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <ScrollView
            android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:id="@+id/description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </ScrollView>
    
        <TextView
            android:id="@+id/solutions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    And then in your activity:

    recyclerView.setOnTouchListener(new View.OnTouchListener() {
    
        public boolean onTouch(View v, MotionEvent event) {
            findViewById(R.id.scroll_view).getParent().requestDisallowInterceptTouchEvent(false);
            return false;
        }
    });
    scrollView.setOnTouchListener(new View.OnTouchListener() {
    
        public boolean onTouch(View v, MotionEvent event) {
            // Disallow the touch request for parent scroll on touch of child view
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });