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.
<attr name="layoutManager" format="string" /> <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"> <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.
Darish
source share