【问题标题】:Scrollbar not showing in RecyclerView滚动条未在 RecyclerView 中显示
【发布时间】:2015-02-07 04:25:50
【问题描述】:

我有一个RecyclerView,希望在滚动条覆盖多个页面时显示滚动条。

我根本没有滚动条。有什么想法吗?

我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/cl_only_empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="5dp"
        android:text="@string/cl_only_empty"
        android:textColor="@color/white" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/callsList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</LinearLayout>

【问题讨论】:

  • 这看起来是正确的。您确定没有主题问题或任何使滚动条与背景颜色相同的问题吗?有没有我们可以查看的示例项目?
  • @Michael Schmidt 这应该可以,只要记住当你想用recyclerview显示scrollbar时总是使用android:scrollbars="vertical"
  • 也许您需要将android:scrollbarStyle="outsideOverlay" 添加到您的RecyclerView
  • 你解决了吗?
  • 找到一篇关于滚动条样式的好文章(同时适用于滚动条和 RecyclerView)...androidopentutorials.com/android-vertical-scrollbar-styling

标签: android android-layout android-recyclerview android-scrollbar


【解决方案1】:

使用 xml android:fadeScrollbars="false"

使用 java ScrollView.setScrollbarFadingEnabled(false);

【讨论】:

    【解决方案2】:

    其实是因为主题。滚动条在那里,但看不到,因为它们与父视图或其祖先行中的某些其他视图的颜色混合。

    您可以创建一个可绘制的形状并为其指定所需的颜色,然后将其设置为可绘制的垂直或水平滚动条。也就是说,如果您不想弄乱您的主题颜色。

    【讨论】:

      【解决方案3】:

      Scroller 可以通过多种方式设置为 recyclerview。 首先,您可以简单地在 xml 中添加滚动条并将其属性 android:fadeScrollbars="false" 设置为始终显示它。

      <android.support.v7.widget.RecyclerView
              android:id="@+id/recyclerViewMachine"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
               android:scrollbars="vertical"
              android:fadeScrollbars="false"
              android:scrollbarThumbVertical="@android:color/darker_gray"
              android:scrollbarSize="5dp"
              android:scrollbarStyle="outsideOverlay"/>
      

      或者你可以制作一个样式主题并在初始化recyclerview时以编程方式使用它

        <style name="ScrollbarRecyclerView" parent="android:Widget">
              <item name="android:scrollbars">vertical</item>
          </style>
      
      RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(context, R.style.ScrollbarRecyclerView));
      

      谢谢

      【讨论】:

        【解决方案4】:

        将代码添加到 Recycler 视图 xml 以使滚动条可见。

        <android.support.v7.widget.RecyclerView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fadeScrollbars="false"
         android:scrollbars="vertical"
         android:scrollbarSize="@dimen/_2sdp"
         android:scrollbarThumbVertical="@color/white"/>
        

        【讨论】:

          【解决方案5】:

          使用 android:scrollbars 属性 "vertical" 和 android:scrollbarThumbVertical 属性来设置颜色和 android:scrollbarSize 属性来指定大小:

          <android.support.v7.widget.RecyclerView
                  android:id="@+id/document_listview"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginBottom="3dp"
                  android:layout_marginTop="3dp"
                  android:scrollbars="vertical"
                  android:scrollbarThumbVertical="@android:color/darker_gray"
                  android:scrollbarSize="5dp"
                  android:background="@color/activity_bg"
                  android:dividerHeight="4dp" />
          

          【讨论】:

          • 注意:在某些设备上将背景设置为某种颜色甚至透明是必不可少的(某些 8.1 平板电脑出现问题)!
          【解决方案6】:

          除了 android:scrollbars 属性,你应该像这样在 false 状态下添加 android:fadeScrollbars 属性:

          <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            android:fadeScrollbars="false"/>
          

          这样,垂直滚动条在使用更多允许的布局高度时始终显示。

          【讨论】:

            【解决方案7】:

            如果您可以通过编程方式设置 ScrollBar,那么您可以使用ContextThemeWrapper。首先你需要在 Style.xml 文件中定义样式:

            <style name="ScrollbarRecyclerView" parent="android:Widget">
                <item name="android:scrollbars">vertical</item>
            </style>
            

            然后在初始化 RecylerView 时应用样式:

            RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(context, R.style.ScrollbarRecyclerView));
            

            【讨论】:

              【解决方案8】:

              解决方法是在xml布局中设置垂直(或水平)滚动条:

              <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scrollbars="vertical" />
              

              【讨论】:

              • 这不是和问题里的一样吗?
              • -1 正如@Pang 已经指出的那样,OP 在他的代码中已经有了“android:scrollbars”。我的代码中也有这个,仍然没有滚动条...
              • 也许对 OP 来说是另外一回事,但通过添加 android:scrollbars="vertical" 它出现在我面前。
              【解决方案9】:

              我只在旧的 HTC Desire X (api 16) 上遇到了同样的问题。
              我不知道为什么,但如果未设置 android:background 属性,RecyclerView 的 scollbars 在此设备上无法正常工作。尝试将其设置为任何颜色或透明 - 它适用于我,希望它也可以帮助你

              【讨论】:

              • 我能够在不同的模拟器上重现它,为 RecyclerView 添加背景确实有帮助!原始问题的正确答案应该是同时设置android:scrollbarsandroid:background 属性。
              【解决方案10】:

              在xml布局中使用recyclerView如下

                 <android.support.v7.widget.RecyclerView
                  android:id="@+id/recyclerView"
                  android:scrollbars="vertical"
                  android:fadeScrollbars="true"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"  />
              

              并在java中为scrollview添加下面的代码,就可以了

               RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
               recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
               recyclerView.setHasFixedSize(true);
              

              【讨论】:

              • setHasFixSize 用于修复一堆数据。
              【解决方案11】:

              试试这个:

              mLayoutManager = new LinearLayoutManager (this);  
              mLayoutManager.setSmoothScrollbarEnabled (true);
              

              【讨论】:

                【解决方案12】:

                您可以使用 from :

                setScrollbarFadingEnabled(boolean)
                

                Scrollbar Link

                【讨论】:

                • 显而易见的替代方案是布局定义中的android:fadeScrollbars="false"
                猜你喜欢
                • 1970-01-01
                • 2017-12-01
                • 2012-02-07
                • 1970-01-01
                • 2016-05-14
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多