Scrollbar not showing in RecyclerView

杀马特。学长 韩版系。学妹 提交于 2019-11-28 08:02:16
jujux789

The solution is to set the vertical (or horizontal) scrollbar in the xml layout:

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

Use android:scrollbars attribute "vertical" and android:scrollbarThumbVertical attribute to set the color and android:scrollbarSize attribute to specifiy size:

<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" />

use recyclerView as below in xml layout

   <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"  />

and add below code for scrollview in java, it will be okay

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

You can use from :

setScrollbarFadingEnabled(boolean)

Scrollbar Link

mio4kon

Try this:

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

I had the same problem on my old HTC Desire X (api 16) only.
I don't know why, but scollbars of RecyclerView doesn't work properly on this device if the android:background property is not set. Try to set it to any color or to transparent - it works for me, hope it helps you too

Besides the android:scrollbars attribute, you should add android:fadeScrollbars attribute in false state like this:

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

This way, the vertical scrollbar is always showing when it's using more height of the layout permitted.

If you are fine to set ScrollBar programmatically, then you can use ContextThemeWrapper. First you need to define styling in Style.xml file:

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

And then apply styling when you initialize your RecylerView:

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

Add the code to Recycler view xml to make scroll bar visible.

<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"/>

Scroller can be set to recyclerview on multiple ways. 1st you can simply add scrollbar in xml and set its property android:fadeScrollbars="false" to always show it.

<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"/>

Or you can make a style theme and use it programitically when initializing recyclerview

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

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

thanks

Actually, it's because of the theme. The scrollbars are there but can't be seen because they blend with the color on the parent view or some other view in the line of its ancestors.

You can create a drawable shape and give it the color you want then set that as the vertical or horizontal scrollbar drawable. That is, if you do not want to mess around with your theme colors.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!