How to set RecyclerView layoutManager from XML?
<android.support.v7.widget.RecyclerView
app:layoutManager="???"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
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 for GridLayoutManager
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.
if you want use it with LinearLayoutManager
<android.support.v7.widget.RecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" >
that equivalent to
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
And I came here looking for androidx version though it was pretty easy to figure out, here it is
LinearLayoutManager:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Example:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
GridLayoutManager:
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
Example:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:spanCount="2"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>
As you can see in examples above you can control the orientation from within xml using
android:orientation="vertical"
and
android:orientation="horizontal"
And to set the number of columns for GridLayoutManager using
app:spanCount="2"
The most common ones that I use are:
<androidx.recyclerview.widget.RecyclerView
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:listitem="#layout/grid_item"
android:orientation="vertical" app:spanCount="3"/>
And:
<androidx.recyclerview.widget.RecyclerView
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/grid_item"
android:orientation="vertical"/>
It's recommended to set listitem , so that you'd see how it could look in the preview of the layout editor.
If you want to have the order reversed though, I think you have to do it in code instead, and use "tools" in XML if you really want to see something...
This worked for me - just add app:layoutManager="LinearLayoutManager" and you're good to go
<android.support.v7.widget.RecyclerView
android:id="#+id/recordItemList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clipToPadding="false"
android:scrollbars="none"
app:layoutManager="LinearLayoutManager"
app:stackFromEnd="true"
app:reverseLayout="true"/>
You can set the layout manager for recyclerview like this, app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Example
implementation 'com.android.support:recyclerview-v7:28.0.0'
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
/>
layoutManager can be android.support.v7.widget.LinearLayoutManager, android.support.v7.widget.GridLayoutManager
[Read more here]
Related
How to set RecyclerView layoutManager from XML?
<android.support.v7.widget.RecyclerView
app:layoutManager="???"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
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 for GridLayoutManager
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.
if you want use it with LinearLayoutManager
<android.support.v7.widget.RecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" >
that equivalent to
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
And I came here looking for androidx version though it was pretty easy to figure out, here it is
LinearLayoutManager:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Example:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
GridLayoutManager:
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
Example:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:spanCount="2"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>
As you can see in examples above you can control the orientation from within xml using
android:orientation="vertical"
and
android:orientation="horizontal"
And to set the number of columns for GridLayoutManager using
app:spanCount="2"
The most common ones that I use are:
<androidx.recyclerview.widget.RecyclerView
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:listitem="#layout/grid_item"
android:orientation="vertical" app:spanCount="3"/>
And:
<androidx.recyclerview.widget.RecyclerView
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/grid_item"
android:orientation="vertical"/>
It's recommended to set listitem , so that you'd see how it could look in the preview of the layout editor.
If you want to have the order reversed though, I think you have to do it in code instead, and use "tools" in XML if you really want to see something...
This worked for me - just add app:layoutManager="LinearLayoutManager" and you're good to go
<android.support.v7.widget.RecyclerView
android:id="#+id/recordItemList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clipToPadding="false"
android:scrollbars="none"
app:layoutManager="LinearLayoutManager"
app:stackFromEnd="true"
app:reverseLayout="true"/>
You can set the layout manager for recyclerview like this, app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Example
implementation 'com.android.support:recyclerview-v7:28.0.0'
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
/>
layoutManager can be android.support.v7.widget.LinearLayoutManager, android.support.v7.widget.GridLayoutManager
[Read more here]
I have implemented RecyclerView with sticky headers and I did it through ItemDecoration.
It works as expected in the case of stand-alone recycler view.
<LinearLayout ...>
<androidx.recyclerview.widget.RecyclerView .../>
<androidx.recyclerview.widget.RecyclerView ... /> // ItemDecoration is supposed to be here and it works excellent
</LinearLayout>
But I have two lists and I need to use NestedScrollView
<androidx.core.widget.NestedScrollView ...>
<LinearLayout ...>
<androidx.recyclerview.widget.RecyclerView .../>
<androidx.recyclerview.widget.RecyclerView ... /> // It doesn't work here
</LinearLayout>
</androidx.core.widget.NestedScrollView ...>
In this case ItemDecoration doesn't work.
I've found out next information:
onDrawOver's are not called on scrolling because RecyclerView.draw() is not called as well.
All items are created at same time (so Adapter creates view holders for all items in the data). It's bad, but it's not my main problem.
I tried to force calling recyclerview's redraw on scrolling but it doesn't work
Do you have any advice how to fix it?
UPD xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black_background_color"
android:focusableInTouchMode="true"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/menuNewsList"
android:layout_width="match_parent"
android:layout_height="170dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
Code initialization:
listMenu.apply {
adapter = dishAdapter
isNestedScrollingEnabled = false
}
listMenu.addItemDecoration(HeaderItemDecoration(listMenu, isHeader = isHeader()))
HeaderItemDecoration was copied from there
How can I make sticky headers in RecyclerView? (Without external lib)
Shouldn't you add an ItemDecoration to a RecyclerView using the addDecoration(ItemDecoration decoration) method?
I wrote an application and home page is LinearLayout and is in Fragment. In this LinearLayout there are two RecyclerViews and SearchBar etc. I want to scroll whole activity. I spent 2 days for it but cannot succeed. How can I do that in easy way? There are lots of adapters and connections in that LinearLayout. How can I achieve that without broke any code.
I want to scroll whole activity.
Thanks in advance.
Set Scroll/NestedScrollView as a parent view in xml to scroll whole layout.
Add below attribute in recycler view to stop recycler scroll:
android:overScrollMode="never"
Try below code:
<android.support.v4.widget.NestedScrollView
android:id="#+id/navigation_nested"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/margin_15">
<TextView
android:id="#+id/video_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_15"
android:drawableLeft="#drawable/ic_video_tutorial"
android:drawablePadding="#dimen/margin_20"
android:fontFamily="#font/poppins"
android:paddingLeft="#dimen/text_15"
android:text="#string/videos"
android:textColor="#color/blackTextColor"
android:textSize="#dimen/text_15" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
You just add this line:
//java
yourRecyclerView.setLayouManager(new LinearLayoutManager(this));
//kotlin
yourRecyclerView.layoutManager = LinearLayoutManager(this)
or from xml:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Notice that there is not only LinearLayoutManager. If you are using a Grid RecyclerView than you might want to use GridLayoutManager
I solved the problem.
Just write ScrollView over the LinearLayout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout>
.....
</LinearLayout>
</ScrollView>
I'm making an app, which contains a recyclerView with some items in it. The app uses a BottomNavigationView, and three other fragments to show some content.
Now, one of those fragments contains just a RecyclerView and isn't scrolling.
I already tried some things, like changing the height of the RecyclerView from match_parent to wrap_content, wrapping it with a LinearLayout, using a NestedScrollView or regular ScrollView, adding app:layout_behavior and even android:scrollbars. Nothing worked though.
The XML file of the layout with the list which isn't scrolling in it:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="55dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_users"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
I set the ConstrainedLayout below as the parent of the other Fragment, this also contains the BottomNavigationView (this is generated by Android Studio when I created the project btw):
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="#color/colorPrimary"
app:itemIconTint="#drawable/bottom_nav_selector"
app:itemTextColor="#drawable/bottom_nav_selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
Finally, this is inside the Fragment's class, where I set the RecyclerView:
adapter = new UserListAdapter(list, getContext(), getLayoutInflater(), container);
RecyclerView recyclerView = v.findViewById(R.id.rv_users);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
I hope one of you guys can help me!
Have you enough data to list on recycler for scrolling?
I found out how to preview a list item by using
tools:listitem="#layout/my_item_layout"
Android studio however is previewing the recyclerview as a vertical list. Is there a way to tell Android Studio to display the layout preview in a horizontal fashion using LinearLayoutManager?
Add a LayoutManager and set a horizontal orientation.
Here an example:
<android.support.v7.widget.RecyclerView
android:id="#+id/homesRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:layout_centerVertical="true"
/>
If you are using androidx libraries:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/homesRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_centerVertical="true"
/>
Just use in your layout the app:layoutManager and the android:orientation attributes and add them also using the tools namespace.
Something like:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/ly_item"
tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:orientation="horizontal"
../>
the accepted answer works accurately. For androidx just use this in your recyclerView
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="horizontal"
AndroidX
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:listitem="#layout/item"
tools:orientation="horizontal"/>
[Read more here]
Otherwise you can use in java file :
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));