【问题标题】:RecyclerView's scrollbar not appearing on edge of screenRecyclerView 的滚动条没有出现在屏幕边缘
【发布时间】:2020-02-10 10:33:39
【问题描述】:

我正在尝试在我的RecyclerView 周围添加一个边距,同时启用它可以垂直滚动,但由于某种原因,滚动条不会将自身定位在屏幕的末尾/右侧。这是 Android X 特有的问题吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/myLinearLayout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingStart="@dimen/activity_horizontal_margin"
        android:paddingEnd="@dimen/activity_horizontal_margin">

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"/>

</LinearLayout>

【问题讨论】:

    标签: android kotlin android-recyclerview android-scrollview androidx


    【解决方案1】:

    这是 Android X 特有的问题吗?

    不是真的。

    这里的问题是您在 RecyclerView 的容器上指定填充。

    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    

    这是因为用于绘制滚动条的矩形仅限于绘制 RecyclerView 的矩形。这也是因为滚动条限制在顶部的原因。

    删除这些,您将获得位于屏幕边框上的滚动条。

    也检查this related question

    【讨论】:

    • 仍然需要在 RecyclerView 周围填充而不移动滚动条
    • 在这种情况下,应将填充添加到适配器项中
    • 或者,不确定它是否有效,但您可以尝试android:scrollbarStyle="outsideInset" stackoverflow.com/a/23421022/1797950
    【解决方案2】:

    解释:

    栏在recyclerview中,recycler受padding约束。

    快速修复:

    为 recylerview 添加填充

    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/myLinearLayout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin">
    
        <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/myRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical"
                android:paddingEnd.../>
    
    </LinearLayout>
    

    良好做法: 列表应该是全宽的,这样用户交互就不会受到未知间距的阻碍。将填充添加到您在适配器中充气的项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多