Theme level recyclerView

I am trying to implement general style options for RecyclerView at theme levels. Since unlike ListView , I used something like this:

Defined style:

 <style name="StyleListView" parent="Widget.AppCompat.ListView"> <item name="android:requiresFadingEdge">vertical</item> <item name="android:fadingEdgeLength">10dp</item> <item name="android:scrollbars">vertical</item> </style> 

and later used in my custom theme:

 <item name="android:listViewStyle">@style/StyleListView</item> 

This works great for ListView . However, I cannot reflect this for the RecyclerView , as I thought it would work for any type of list.

So, is there any predefined style attribute for RecyclerView , for example android:recyclerViewStyle or something else?

If not, how can I achieve this at the topic level?

+11
android listview styles themes android-recyclerview
source share
4 answers

There is no equivalent listViewStyle for a RecylerView , unfortunately.

I think the best you can do is define a style for your RecyclerView , and then use any views you want to use in this style, style="@style/RecyclerViewStyle" (if you just want to define a couple of attributes as in your example).

If you really just don't want to do this for each RecyclerView , then you will have to subclass it and return a nonzero parameter for defStyle in the constructor. However, you will have to replace all instances of your RecyclerView in XML with your new subclass class.

+9
source share

Theme level RecyclerView

RecyclerView now has a default style attribute: recyclerViewStyle , which allows you to set a default style in your theme

The following attributes are supported.

  <!-- Class name of the Layout Manager to be used. --> <attr name="layoutManager" format="string" /> <!-- ============================= --> <!-- Attributes for Layout Manager --> <!-- ============================= --> <attr name="android:orientation" /> <attr name="android:descendantFocusability" /> <attr name="android:clipToPadding" /> <attr name="spanCount" format="integer"/> <attr name="reverseLayout" format="boolean" /> <attr name="stackFromEnd" format="boolean" /> <attr name="fastScrollEnabled" format="boolean" /> <attr name="fastScrollVerticalThumbDrawable" format="reference" /> <attr name="fastScrollVerticalTrackDrawable" format="reference" /> <attr name="fastScrollHorizontalThumbDrawable" format="reference" /> <attr name="fastScrollHorizontalTrackDrawable" format="reference" /> 

To use the above function, add / update the following dependency in the build.gradle file.

 dependencies { implementation 'androidx.recyclerview:recyclerview:1.1.0-beta05' } 

You can see the commit here .

Example:

1. Define your default style

 <style name="DefaultRecyclerViewStyle"> <item name="layoutManager">androidx.recyclerview.widget.GridLayoutManager</item> <item name="android:orientation">vertical</item> <item name="spanCount">2</item> </style> 

2. Add the above style to the default theme

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="recyclerViewStyle">@style/DefaultRecyclerViewStyle</item> </style> 

That's all. You have defined the default application style for your RecyclerView widgets.

+3
source share

As @Jason Robinson says you can subclass RecyclerView this way, replace RecyclerView with RecyclerViewStyleable in xml, and then create it with recyclerViewStyle

 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="RecyclerViewStyleable"> <attr name="recyclerViewStyle"/> </declare-styleable> </resources> 

-

 public class RecyclerViewStyleable extends RecyclerView { public RecyclerViewStyleable(Context context) { super(context); } public RecyclerViewStyleable(Context context, AttributeSet attrs) { this(context, attrs, R.attr.recyclerViewStyle); } public RecyclerViewStyleable(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } } 
+1
source share

According to this changelog on August 7, 2019, RecyclerView adds a recyclerViewStyle style attribute that behaves like listViewStyle .

Add it to your build.gradle :

 dependencies { ... implementation 'androidx.recyclerview:recyclerview:1.1.0-beta02' ... } 

Note. You need to use AndroidX, which is part of Android Jetpack.

0
source share

All Articles