recyclerview未在nestedscrollview内滚动

lndjwyie  于 2021-07-07  发布在  Java
关注(0)|答案(3)|浏览(315)

我正在设计一个复杂的ui,它有一个嵌套的ScrollView作为父级,在它里面有一个recyclerview。现在的问题是我无法滚动recyclerview中的项目。
我试过的东西
用nestedscrollview替换scrollview
有可能android:fillviewport=“正确”
禁用recyclerview的nestedscrolling

<?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"
    tools:context=".screens.StudentPreview">

    <androidx.cardview.widget.CardView
        android:id="@+id/toolbarCV"
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:translationY="-45dp"
        app:cardCornerRadius="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#1641A2">

            <ImageView
                android:id="@+id/menuIV"
                android:layout_width="23dp"
                android:layout_height="23dp"
                android:layout_gravity="center"
                android:layout_marginStart="24dp"
                android:layout_marginLeft="24dp"
                android:src="@drawable/ic_home"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.7" />

            <TextView
                android:id="@+id/featureName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:fontFamily="@font/poppins_light"
                android:text="Student Preview"
                android:textColor="#fff"
                android:textSize="22sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.75" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/studentPreviewNSV"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="90dp"
        android:fillViewport="true"
        android:visibility="gone"
        app:layout_constraintTop_toTopOf="parent"
        tools:visibility="visible">

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

            <androidx.cardview.widget.CardView
                android:id="@+id/avatarCV"
                android:layout_width="60dp"
                android:layout_height="60dp"

                app:cardBackgroundColor="@color/colorPrimaryDark"
                app:cardCornerRadius="40dp"
                app:cardElevation="0dp"
                app:layout_constraintBottom_toBottomOf="@+id/studentDetailsCL"
                app:layout_constraintEnd_toStartOf="@+id/studentDetailsCL"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintHorizontal_chainStyle="packed"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/studentDetailsCL">

                <ImageView
                    android:id="@+id/avatarIV"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:visibility="gone"
                    tools:src="@drawable/avatar_sample" />

                <TextView
                    android:id="@+id/nameInitialTV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:fontFamily="@font/montserrat"
                    android:textColor="#FFF"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    android:visibility="gone"
                    tools:text="AK"
                    tools:visibility="visible" />

            </androidx.cardview.widget.CardView>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/studentDetailsCL"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginLeft="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toEndOf="@+id/avatarCV"
                app:layout_constraintTop_toTopOf="parent">

                <TextView
                    android:id="@+id/studentNameTV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/montserrat"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:text="Akrit Khanna" />

                <TextView
                    android:id="@+id/classNameTV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/montserrat"
                    android:textColor="#000"
                    android:textSize="15sp"
                    app:layout_constraintStart_toStartOf="@+id/studentNameTV"
                    app:layout_constraintTop_toBottomOf="@+id/studentNameTV"
                    tools:text="Class 8th A" />

                <TextView
                    android:id="@+id/lastLoggedTV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="1dp"
                    android:fontFamily="@font/raleway"
                    android:textColor="#000"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    app:layout_constraintStart_toStartOf="@+id/classNameTV"
                    app:layout_constraintTop_toBottomOf="@+id/classNameTV"
                    tools:text="Last Logged: 30-01-2020" />

            </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.cardview.widget.CardView
                android:id="@+id/classEnrollCV"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_marginStart="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginEnd="5dp"
                android:layout_marginRight="5dp"
                app:cardBackgroundColor="#F5F5F5"
                app:cardCornerRadius="18dp"
                app:cardElevation="3dp"
                app:layout_constraintEnd_toStartOf="@+id/courseEnrollCV"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintHorizontal_chainStyle="packed"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/detailsGL">

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

                    <TextView
                        android:id="@+id/classEnrollCountTV"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/raleway"
                        android:textColor="#000"
                        android:textSize="22sp"
                        android:textStyle="bold"
                        app:layout_constraintBottom_toTopOf="@+id/classEnrollTVContainerFL"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        tools:text="1" />

                    <FrameLayout
                        android:id="@+id/classEnrollTVContainerFL"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:background="#D0D9EC"
                        app:layout_constraintBottom_toBottomOf="parent">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:fontFamily="@font/raleway"
                            android:text="Classes Enrolled"
                            android:textColor="#1641A2"
                            android:textSize="18sp" />

                    </FrameLayout>
                </androidx.constraintlayout.widget.ConstraintLayout>

            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/courseEnrollCV"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_marginStart="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                app:cardBackgroundColor="#F5F5F5"
                app:cardCornerRadius="18dp"
                app:cardElevation="3dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toEndOf="@+id/classEnrollCV"
                app:layout_constraintTop_toTopOf="@+id/detailsGL">

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

                    <TextView
                        android:id="@+id/courseEnrollCountTV"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/raleway"
                        android:textColor="#000"
                        android:textSize="22sp"
                        android:textStyle="bold"
                        app:layout_constraintBottom_toTopOf="@+id/courseEnrollTVContainerFL"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        tools:text="1" />

                    <FrameLayout
                        android:id="@+id/courseEnrollTVContainerFL"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:background="#D0D9EC"
                        app:layout_constraintBottom_toBottomOf="parent">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:fontFamily="@font/raleway"
                            android:text="Courses Enrolled"
                            android:textColor="#1641A2"
                            android:textSize="18sp" />

                    </FrameLayout>
                </androidx.constraintlayout.widget.ConstraintLayout>

            </androidx.cardview.widget.CardView>

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

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/reportCL"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:background="#F5F5F5"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/classEnrollCV">

                <Spinner
                    android:id="@+id/classOrTermSpinner"
                    style="@style/Base.Widget.AppCompat.Spinner.Underlined"
                    android:layout_width="0dp"
                    android:layout_height="50dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp"
                    app:layout_constraintEnd_toStartOf="@+id/courseSpinner"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintHorizontal_chainStyle="packed"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <Spinner
                    android:id="@+id/courseSpinner"
                    style="@style/Base.Widget.AppCompat.Spinner.Underlined"
                    android:layout_width="0dp"
                    android:layout_height="50dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintStart_toEndOf="@+id/classOrTermSpinner"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/reportRV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/courseSpinner" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

6ovsh4lw

6ovsh4lw1#

把里面的所有视图都包起来 NestedScrollView 变成一个 RelativeLayout . 对于前,

<androidx.core.widget.NestedScrollView
        android:id="@+id/studentPreviewNSV"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="90dp"
        android:fillViewport="true"
        android:visibility="gone"
        app:layout_constraintTop_toTopOf="parent"
        tools:visibility="visible">

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

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

                <!-- your remaining code -->

            </androidx.constraintlayout.widget.ConstraintLayout>
       </RelativeLayout>
   </androidx.core.widget.NestedScrollView>
tp5buhyn

tp5buhyn2#

使用 NestedScrolView 并将此添加到您的recyclerview:

recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);

或将此转换为xml:

android:nestedScrollingEnabled="false"

此外,还可以为recyclerview添加方向:

android:orientation="vertical"
az31mfrm

az31mfrm3#

我设法通过添加android:layout_marginbottom=“100dp”我想这是因为我android:layout_margintop=“90dp”到我的nestedscrollview,由于recyclerview假设所有项目都在屏幕上可见,因此不需要滚动(如果我错了,请纠正我)

<androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/reportRV"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/courseSpinner"
                    tools:listitem="@layout/report_list_layout"
                    android:orientation="vertical"
                    android:layout_marginBottom="100dp"/>

相关问题