简体   繁体   中英

ScrollView inside recycler view item is not scrolling

I have MetarialCardView which has a certain height inside of RecyclerView's list item and I have a scrollView inside this MetarialCardView. I want to scroll by putting a long text in the ScrollView, but it is not scrolling. Besides that typically MaterialCardView is clickable, but with scrollView, it is losing its clickability too.

I appreciate any help you can provide.

<com.google.android.material.card.MaterialCardView 
    android:layout_width="match_parent"
    android:layout_height="130dp"
    app:cardCornerRadius="12dp"
    app:strokeColor="@color/primary_text_color_50"
    app:strokeWidth="0.5dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/clContract"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/ivCompleted"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="8dp"
            android:src="@drawable/ic_completed"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tvContract"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="8dp"
            android:fontFamily="@font/gotham_rounded_bold"
            android:textColor="@color/primary_text_color"
            android:textSize="@dimen/text_12"
            app:layout_constraintEnd_toStartOf="@+id/ivCompleted"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Agreement" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

    <View
        android:id="@+id/viewGradient"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_gravity="bottom"
        android:background="@drawable/shape_gradient_bottom_separator" />

</com.google.android.material.card.MaterialCardView>

Try using NestedScrollview instead ScrollView in xml and below code in java file

    recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            int action = e.getAction();
            switch (action) {
            case MotionEvent.ACTION_MOVE:
                rv.getParent().requestDisallowInterceptTouchEvent(true);
                break;
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
});

Try this one. I also removed the icon from scroll view if you want to scroll that to just put it inside the scrollview.

for click i think you can set click listener on itemView or just give id to cardview and set click listener on that.

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 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="130dp"
    app:cardCornerRadius="12dp"
    app:strokeColor="@color/primary_text_color_50"
    app:strokeWidth="0.5dp">

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

        <ScrollView
            android:id="@+id/scrollView2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/viewGradient"
            app:layout_constraintEnd_toStartOf="@+id/ivCompleted"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/clContract"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <TextView
                    android:id="@+id/tvContract"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="12dp"
                    android:layout_marginEnd="8dp"
                    android:fontFamily="@font/gotham_rounded_bold"
                    android:textColor="@color/primary_text_color"
                    android:textSize="@dimen/text_12"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:text="AgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreement" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </ScrollView>

        <ImageView
            android:id="@+id/ivCompleted"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="8dp"
            android:src="@drawable/ic_completed"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/viewGradient"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_gravity="bottom"
            android:background="@drawable/ic_launcher_background"
            app:layout_constraintBottom_toBottomOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM