CoordinatorLayout 列表为空时无法滑动的原因及解决方法
当 CoordinatorLayout 中的列表内容为空时,不能滑动的原因可能是没有设置合适的布局参数或没有正确配置 CoordinatorLayout 的子视图。
- 布局参数:确保列表视图(如 RecyclerView 或 ListView)的高度设置为'match_parent',这样它才能占据 CoordinatorLayout 的整个空间,并且可以滑动。
例如,使用 RecyclerView 作为列表视图:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
- 子视图配置:确保 CoordinatorLayout 中的子视图正确配置了'behavior'属性。这个属性决定了子视图在 CoordinatorLayout 中的行为,如滑动、吸附等。
例如,如果你想在 CoordinatorLayout 中使用 AppBarLayout 和 CollapsingToolbarLayout 实现可折叠的标题栏效果,你需要将 AppBarLayout 的'behavior'属性设置为'AppBarLayout.ScrollingViewBehavior',这样列表内容才能正确滚动:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!-- 标题栏内容 -->
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
确保子视图的'behavior'属性正确设置,可以让 CoordinatorLayout 正确处理滑动事件,使得列表内容能够滑动。
原文地址: https://www.cveoy.top/t/topic/qwr7 著作权归作者所有。请勿转载和采集!