【问题标题】:NestedScrollView inside ConstraintLayout not scrollableConstraintLayout 内的 NestedScrollView 不可滚动
【发布时间】:2022-01-02 16:13:08
【问题描述】:

我有一个ConstraintLayout,里面还有几个其他部分也是ConstraintLayouts。最后在底部我有两个RecyclerViews 和一对按钮。我的目标是让layout_2 下方的屏幕部分可滚动——这样就包括两个RecyclerViews 和几个附带的标签。然后这两个按钮必须始终可见。

代码:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/picker_performance_grey"
    android:padding="20dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layout_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@color/colorWhite"
        android:padding="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <!--        some widgets-->

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layout_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@color/colorWhite"
        android:padding="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/layout_1">

        <!--        some widgets-->

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        app:layout_constraintTop_toBottomOf="@id/layout_2"
        app:layout_constraintStart_toStartOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!--            some widgets-->

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_pick"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                tools:listitem="@layout/inbound_item_detail_sector"
                tools:visibility="visible" />

            <!--            some widgets-->

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_buffer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                tools:listitem="@layout/inbound_item_detail_sector"
                tools:visibility="gone" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <Button
            android:id="@+id/btn_1"
            style="@style/button_bordered"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="5dp"
            android:layout_weight="50" />

        <Button
            android:id="@+id/btn_2"
            style="@style/button_bordered"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="5dp"
            android:layout_weight="50"
            android:text="@string/product_detail_btn_finish" />

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

但是,滚动不起作用。我做错了什么?

【问题讨论】:

    标签: android android-recyclerview android-nestedscrollview


    【解决方案1】:

    尝试在 recyclerview for ex 中停止 nestedScrolling

     <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_pick"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:nestedScrollingEnabled="false"
                    tools:listitem="@layout/inbound_item_detail_sector"
                    tools:visibility="visible" />
    

    或来自代码rv_pick.setNestedScrollingEnabled(false);

    如果只出现一个回收器而另一个将消失,我建议在 XML 中只使用一个并通过更改适配器从 java 代码控制它

    【讨论】:

    • 试过了,可惜没用。
    猜你喜欢
    • 2019-12-23
    • 1970-01-01
    • 2019-10-17
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    相关资源
    最近更新 更多