RecyclerView inside a RecyclerView

雨燕双飞 提交于 2021-02-04 21:34:21

问题


I have this RecyclerView, which in it every item has a RecyclerView inside of it:

fragment_active_goals.xml (contains the outer recyclerview):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.ActiveGoalsFragment">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/light_gray"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:nestedScrollingEnabled="false"
                android:scrollbars="vertical" />


        </RelativeLayout>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="20dp"
            android:src="@drawable/plus"
            app:fabSize="normal" />

        <com.github.mmin18.widget.RealtimeBlurView
            android:id="@+id/blur_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:realtimeBlurRadius="20dp"
            app:realtimeOverlayColor="#8000"
            android:elevation="5dp"
            android:visibility="gone"/>

        <com.google.android.material.circularreveal.CircularRevealFrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:elevation="10dp"
            android:layout_margin="15dp"
            android:visibility="invisible"
            app:layout_behavior="com.google.android.material.transformation.FabTransformationSheetBehavior">

            <com.google.android.material.card.MaterialCardView
                android:id="@+id/dialog"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                app:cardCornerRadius="15dp"
                app:cardPreventCornerOverlap="true">

                <RelativeLayout
                    android:id="@+id/pr"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/add_new_active_activity_dialog_background"
                    android:gravity="center"
                    android:padding="15dp">

                    <TextView
                        android:id="@+id/name_label"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="@string/name_of_new_goal_label_dialog"
                        android:textSize="20sp" />

                    <EditText
                        android:id="@+id/name_et"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/name_label"
                        android:hint="@string/name_of_new_goal_hint_dialog"
                        android:textSize="20sp" />

                    <TextView
                        android:id="@+id/description_label"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/name_et"
                        android:layout_gravity="center"
                        android:text="@string/description_of_new_goal_label_dialog"
                        android:textSize="20sp" />

                    <EditText
                        android:id="@+id/description_et"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/description_label"
                        android:hint="@string/description_of_new_goal_hint_dialog"
                        android:textSize="20sp" />

                    <RelativeLayout
                        android:id="@+id/cancel_btn"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/description_et"
                        android:layout_alignStart="@id/name_et"
                        android:layout_margin="15dp"
                        android:background="@drawable/ripple_button_background"
                        android:clickable="true"
                        android:elevation="8dp"
                        android:focusable="true"
                        android:padding="15dp">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/cancel_adding_new_goal_dialog_button_text"
                            android:textAllCaps="false"
                            android:textColor="@color/brain1" />

                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/add_btn"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/description_et"
                        android:layout_alignEnd="@id/description_et"
                        android:layout_margin="15dp"
                        android:background="@drawable/ripple_button_background"
                        android:clickable="true"
                        android:elevation="8dp"
                        android:focusable="true"
                        android:padding="15dp">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/add_new_goal_dialog_button_text"
                            android:textAllCaps="false"
                            android:textColor="@color/brain1" />

                    </RelativeLayout>

                </RelativeLayout>

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

        </com.google.android.material.circularreveal.CircularRevealFrameLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</FrameLayout>

outer_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true">


    <FrameLayout
        android:id="@+id/cardHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <androidx.cardview.widget.CardView
            android:id="@+id/acardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            app:cardCornerRadius="15dp"
            app:cardElevation="10dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <RelativeLayout
                    android:id="@+id/aroot_view_per_active_goal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <LinearLayout
                        android:id="@+id/aactive_goal_card"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="4dp"
                        android:orientation="horizontal"
                        android:padding="10dp"
                        android:weightSum="11">

                        <com.example.performancemeasurement.customViews.CustomProgressBar.CustomProgressBar
                            android:id="@+id/aactive_goal_item_progress_bar"
                            android:layout_width="0dp"
                            android:layout_height="50dp"
                            android:layout_weight="9"
                            android:transitionName="progressBar"
                            android:visibility="visible"
                            app:enable_gradient="true" />

                        <ImageButton
                            android:id="@+id/aactive_goal_item_edit_image_button"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:background="?attr/selectableItemBackground"
                            android:contentDescription="shrunk"
                            android:scaleType="fitCenter"
                            android:src="@drawable/edit_vector" />

                        <ImageButton
                            android:id="@+id/aactive_goal_item_open_close_image_button"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:background="?attr/selectableItemBackground"
                            android:contentDescription="shrunk"
                            android:scaleType="fitCenter"
                            android:src="@drawable/down_vector" />

                    </LinearLayout>

                    <RelativeLayout
                        android:id="@+id/aexpanded_active_goal_card"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="4dp"
                        android:visibility="visible">

                        <androidx.constraintlayout.widget.ConstraintLayout
                            android:id="@+id/aactive_goal_item_back_to_parent_image_button_container"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:clickable="false"
                            android:focusable="false"
                            android:layout_alignParentTop="true"
                            android:layout_alignParentStart="true"
                            android:layout_alignBottom="@id/aexpanded_active_goal_card_label">

                            <ImageButton
                                android:id="@+id/aactive_goal_item_back_to_parent_image_button"
                                android:layout_width="0dp"
                                android:layout_height="match_parent"
                                app:layout_constraintDimensionRatio="1:1"
                                android:src="@drawable/ic_arrow_back_black_24dp"
                                android:background="?attr/selectableItemBackground"
                                app:layout_constraintTop_toTopOf="parent"
                                app:layout_constraintStart_toStartOf="parent"/>

                        </androidx.constraintlayout.widget.ConstraintLayout>


                        <TextView
                            android:id="@+id/aexpanded_active_goal_card_label"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="10dp"
                            android:layout_centerHorizontal="true"
                            android:text="Goal Name"
                            android:textColor="@android:color/black"
                            android:textSize="40sp"
                            android:textStyle="bold" />

                        <com.developer.mtextfield.TextFieldBoxes
                            android:id="@+id/aexpanded_active_goal_card_edit_label_field_box"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="10dp"
                            android:layout_centerHorizontal="true"
                            app:labelText="Goal Name:"
                            android:visibility="invisible" >

                            <com.developer.mtextfield.ExtendedEditText
                                android:id="@+id/aexpanded_active_goal_card_edit_label_edit_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"/>

                        </com.developer.mtextfield.TextFieldBoxes>


                        <com.example.performancemeasurement.customViews.CustomProgressBar.CustomProgressBar
                            android:id="@+id/aexpanded_active_goal_card_progress_bar"
                            android:layout_width="wrap_content"
                            android:layout_height="30dp"
                            android:layout_below="@id/aexpanded_active_goal_card_label"
                            android:layout_alignParentStart="true"
                            android:layout_alignParentEnd="true"
                            android:layout_marginStart="20dp"
                            android:layout_marginEnd="20dp"
                            android:layout_centerHorizontal="true"
                            android:transitionName="progressBar"
                            app:enable_gradient="true" />

                        <TextView
                            android:id="@+id/aexpanded_active_goal_card_description"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/aexpanded_active_goal_card_progress_bar"
                            android:layout_centerHorizontal="true"
                            android:layout_margin="10dp"
                            android:maxLines="3"
                            android:text="Goal's Description"
                            android:textColor="#6F6F6F"
                            android:textSize="20sp"
                            android:textStyle="normal" />

                        <com.developer.mtextfield.TextFieldBoxes
                            android:id="@+id/aexpanded_active_goal_card_edit_description_field_box"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/aexpanded_active_goal_card_progress_bar"
                            android:layout_centerHorizontal="true"
                            android:layout_margin="10dp"
                            app:labelText="Goal Description:"
                            android:visibility="invisible" >

                            <com.developer.mtextfield.ExtendedEditText
                                android:id="@+id/aexpanded_active_goal_card_edit_description_edit_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"/>

                        </com.developer.mtextfield.TextFieldBoxes>

                        <LinearLayout
                            android:id="@+id/aexpanded_active_goal_card_sub_goals_label_container"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/aexpanded_active_goal_card_description"
                            android:layout_centerHorizontal="true"
                            android:layout_margin="5dp"
                            android:gravity="center_vertical"
                            android:orientation="horizontal"
                            android:weightSum="14">

                            <View
                                android:id="@+id/aexpanded_active_goal_card_sub_goal_label_left"
                                android:layout_width="0dp"
                                android:layout_height="3dp"
                                android:layout_weight="4"
                                android:background="@color/light_gray" />

                            <TextView
                                android:id="@+id/aexpanded_active_goal_card_sub_goals_label"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_margin="5dp"
                                android:layout_weight="6"
                                android:gravity="center"
                                android:text="SubGoals"
                                android:textColor="#5A5A5A"
                                android:textSize="25sp"
                                android:textStyle="bold" />

                            <View
                                android:id="@+id/aexpanded_active_goal_card_sub_goal_label_right"
                                android:layout_width="0dp"
                                android:layout_height="3dp"
                                android:layout_weight="4"
                                android:background="@color/light_gray" />

                        </LinearLayout>

                        <RelativeLayout
                            android:id="@+id/aexpanded_active_goal_card_sub_goals_container"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/aexpanded_active_goal_card_sub_goals_label_container"
                            android:layout_centerHorizontal="true"
                            android:layout_margin="10dp"
                            android:animateLayoutChanges="true"
                            android:elevation="15dp"
                            android:gravity="center">

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

                        </RelativeLayout>

                    </RelativeLayout>

                </RelativeLayout>

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/edit_panel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="4dp"
                    android:layout_below="@id/aroot_view_per_active_goal"
                    android:visibility="gone">

                    <RelativeLayout
                        android:id="@+id/edit_delete_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="15dp"
                        android:elevation="8dp"
                        android:padding="15dp"
                        android:clickable="true"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"
                        android:focusable="true"
                        android:background="@drawable/delete_goal_button_background">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAllCaps="false"
                            android:textColor="@android:color/white"
                            android:text="@string/active_goal_item_delete_button_text"/>

                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/edit_cancel_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="15dp"
                        android:elevation="8dp"
                        android:padding="15dp"
                        android:clickable="true"
                        app:layout_constraintEnd_toStartOf="@id/edit_save_button"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"
                        android:focusable="true"
                        android:background="@drawable/cancel_edits_button_background">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAllCaps="false"
                            android:textColor="@android:color/white"
                            android:text="@string/active_goal_item_cancel_edits_button_text"/>

                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/edit_save_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="15dp"
                        android:elevation="8dp"
                        android:padding="15dp"
                        android:clickable="true"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"
                        android:focusable="true"
                        android:background="@drawable/save_edits_button_background">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAllCaps="false"
                            android:textColor="@android:color/white"
                            android:text="@string/active_goal_item_save_edits_button_text"/>

                    </RelativeLayout>

                </androidx.constraintlayout.widget.ConstraintLayout>

            </RelativeLayout>

        </androidx.cardview.widget.CardView>


    </FrameLayout>

    <com.github.mmin18.widget.RealtimeBlurView
        android:id="@+id/ablur"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/cardHolder"
        android:layout_alignBottom="@id/cardHolder"
        app:realtimeBlurRadius="20dp"
        app:realtimeOverlayColor="#8000"
        android:visibility="invisible"/>


</RelativeLayout>

For the inner RecyclerView I use this library (this answer explains how the inner RecyclerView works). Right now the inner RecyclerView won't scroll, but instead, the outer one will. How can I make the inner RecyclerView scrollable instead of scrolling the outer RecyclerView?

Also, I want to be able to swipe horizontally too, not only vertically scrolling.

Help would be highly appreciated (:


回答1:


RecyclerView implements NestedScrollingChild2 inteface but does not implement any of NestedScrollingParent* interfaces, so it can be a scrollable child, but cannot be a parent for such a child. To make it work you need to create a subclass of RecyclerView and implement NestedScrollingParent interface yourself. There are some articles also on how to do that, for instance this one.



来源:https://stackoverflow.com/questions/60311373/recyclerview-inside-a-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!