Scrollbar not showing in RecyclerView

2019-01-23 10:43发布

问题:

I've got a RecyclerView and would like to have scrollbar showing, when it covers more than one page.

I get no scrollbar at all. Any idea?

My layout:

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

回答1:

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


回答2:

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


回答3:

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);


回答4:

You can use from :

setScrollbarFadingEnabled(boolean)

Scrollbar Link



回答5:

Try this:

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


回答6:

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



回答7:

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));


回答8:

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.



回答9:

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


回答10:

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



回答11:

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.