Is there a way to enable scrollbars for a RecyclerView in code?

As we have seen, RecyclerView is more efficient than ListView, so I prefer to use it in my project. But lately, I ran into a problem when I put it in my own ViewGroup. RecyclerView easily set scrollbars in xml as follows:

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

But I really cannot find any method for setting scrollbars in the code for RecyclerView, which I tried:

 mRecyclerView.setVerticalScrollBarEnabled(true); 

and then I saw this one in an Android document.

So, I tried to create my own LayoutManager and override the functions that I thought I needed. But in the end I failed. So can anyone tell me how to make my own LayoutManager or just show me another solution. Thank!

+93
android scrollbars android-recyclerview
Nov 21 '14 at 7:39
source share
6 answers

It is currently not possible to program scrollbars. The reason for this behavior is because Android does not call either View.initializeScrollbarsInternal(TypedArray a) or View.initializeScrollbars(TypedArray a) . Both methods are only called if you are instantiating a RecyclerView using an AttributeSet.
As a workaround, I would suggest that you only create a new layout file with RecyclerView: vertical_recycler_view.xml

 <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" /> 

Now you can inflate and add RecyclerView by scrolling wherever you want: MyCustomViewGroup.java

 public class MyCustomViewGroup extends FrameLayout { public MyCustomViewGroup(Context context) { super(context); RecyclerView verticalRecyclerView = (RecyclerView) LayoutInflater.from(context).inflate(R.layout.vertical_recycler_view, null); verticalRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)); addView(verticalRecyclerView); } } 
+112
Dec 04 '14 at 16:20
source share

Set vertical scrollbar in xml layout

 <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" /> 
+63
Sep 11 '15 at 9:03
source share

Just in the xml properties

 <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recyclerView" android:scrollbars="vertical" <!-- type of scrollbar --> android:scrollbarThumbVertical="@android:color/darker_gray" <!--color of scroll bar--> android:scrollbarSize="5dp"> <!--width of scroll bar--> </android.support.v7.widget.RecyclerView> 
+28
Aug 24 '16 at 13:46 on
source share

I would rather use ContextThemeWrapper for this.

First define in Style.xml:

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

And then whenever you initialize your RecyclerView, use ContextThemeWrapper :

 RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(context, R.style.ScrollbarRecyclerView)); 
+11
Jan 30 '17 at 13:48 on
source share

You can do this without inflating the XML layout, but you need to declare a custom theme attribute and style:

 <resources> <attr name="verticalRecyclerViewStyle" format="reference"/> <style name="VerticalRecyclerView" parent="android:Widget"> <item name="android:scrollbars">vertical</item> </style> </resources> 

Then set the style attribute value in your theme:

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="verticalRecyclerViewStyle">@style/VerticalRecyclerView</item> </style> 

Now you can create a RecyclerView programmatically using a vertical scrollbar:

 RecyclerView recyclerView = new RecyclerView(context, null, R.attr.verticalRecyclerViewStyle); 
+7
Nov 27 '16 at 20:40
source share

Use code like:

 <androidx.recyclerview.widget.RecyclerView android:id="@+id/categoryRecyclerLayout" android:layout_width="414dp" android:layout_height="652dp" android:scrollbars="vertical" .... /> 
0
Sep 05 '19 at 13:39 on
source share



All Articles