I made a Horizontal RecyclerView and it works fine(thanks to this) but the direction of scroll and data are expand from left to right; then How can I change the RecyclerView
I have tried all above answers it's showing me same vertically recycler view, so I have tried another example.
Initialize the adapter
private Adapter mAdapter;
set the adapter like this
mAdapter = new Adapter();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recycler_view.setLayoutManager(linearLayoutManager);
recycler_view.setAdapter(mAdapter);
Hope this will also work for you For Complete code please refer this link
Assuming you use LinearLayoutManager
in your RecyclerView, then you can pass true
as third argument in the LinearLayoutManager constructor.
For example:
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
If you are using the StaggeredGridLayoutManager
, then you can use the setReverseLayout
method it provides.
XML approach using androidx:
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/my_recycler_view"
android:orientation="horizontal"
tools:listitem="@layout/my_item"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_height="wrap_content">
For changing the direction of swipe you can use
reverselayout attribute = true.
In Kotlin,
val layoutManager = LinearLayoutManager(this@MainActivity,LinearLayoutManager.HORIZONTAL,true)
recyclerview.layoutManager = layoutManager
In Java,
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true);
recyclerview.setLayoutManager(layoutManager);
Actually it reverses the layout.
If it shows like below
1.2..3....10
it will change to
10.9..8....1
For creating Horizontal RecyclerView there are many ways.
4 Ways To Create Horizontal RecyclerView In Android
Try this in fragment :
layoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false);
mRecyclerView.setLayoutManager(layoutManager);
You can do it with just xml.
the app:reverseLayout="true" do the job!
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:orientation="horizontal"
app:reverseLayout="true"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />