How to set RecyclerView app:layoutManager= from XML?
As you can check in the doc:
Class name of the
Layout Manager
to be used.The class must extend
androidx.recyclerview.widget.RecyclerViewView$LayoutManager
and have either a default constructor or constructor with the signature(android.content.Context, android.util.AttributeSet, int, int)
If the name starts with a
'.'
, application package is prefixed. Else, if the name contains a'.'
, the classname is assumed to be a full class name. Else, the recycler view package (androidx.appcompat.widget
) is prefixed
With androidx you can use:
<androidx.recyclerview.widget.RecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager">
With the support libraries you can use:
<android.support.v7.widget.RecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layoutManager="android.support.v7.widget.GridLayoutManager" >
Also you can add these attributes:
android:orientation
="horizontal|vertical"
: to control the orientation of the LayoutManager (eg:LinearLayoutManager
)app:spanCount
: to set the number of columns forGridLayoutManager
Example:
<androidx.recyclerview.widget.RecyclerView
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
...>
or:
<androidx.recyclerview.widget.RecyclerView
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="vertical"
...>
You can also add them using the tools
namespace (i.e. tools:orientation
and tools:layoutManager
) and then it only impacts the IDE preview and you can continue setting those values in code.
How to build a horizontal ListView with RecyclerView
Is there a better way to implement this now with RecyclerView now?
Yes.
When you use a RecyclerView
, you need to specify a LayoutManager
that is responsible for laying out each item in the view. The LinearLayoutManager
allows you to specify an orientation, just like a normal LinearLayout
would.
To create a horizontal list with RecyclerView
, you might do something like this:
LinearLayoutManager layoutManager
= new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false);
RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);
RecyclerView has no LayoutManager | Kotlin
This error occurs when you've created same layout for multiple SDK or orientation and not mentioned the view in all the layout files, say for: activity_main.xml(v21)or activity_main(land) and so on. All the layouts should contain all the views with same IDs.
FIX: I had another activity_home(v21) layout that was cause the problems. Make sure you don't have multiple layouts that will cause problems. It was detecting no LayoutManager in the v21 version layout.
Related Topics
Multiple Table SQLite Db Adapter(S) in Android
How to Disable Crashlytics During Development
How to Get a Fragment to Remove Itself, I.E. Its Equivalent of Finish()
Nested Fragments Disappear During Transition Animation
Custom Translucent Android Actionbar
Progress Bar While Loading Image Using Glide
Differencebetween Sendstickybroadcast and Sendbroadcast in Android
Get List of Active Pendingintents in Alarmmanager
How to Prevent a Scrollview from Scrolling to a Webview After Data Is Loaded
How to Launch an Activity Without a Ui
How to Hide One Item in an Android Spinner
Live-Stream Video from One Android Phone to Another Over Wifi
Swap Fragment in an Activity via Animation
Android - Way to Appear Bordered Text on the Textview
Android Material Chip Component Crashing App. Unable to Inflate Xml