Android Horizontal RecyclerView scroll Direction
Asked Answered
A

14

75

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 scroll direction like in the picture below?

enter image description here

My Code:

StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(
                        2, //The number of Columns in the grid
                        LinearLayoutManager.HORIZONTAL);
Azriel answered 9/6, 2016 at 18:6 Comment(0)
M
207

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.

Mansion answered 9/6, 2016 at 19:5 Comment(1)
Yes, depending upon the flag that is passing in the constructor the scroll will change. Thanks a lot!Materialize
P
48

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" />
Parvenu answered 9/6, 2016 at 19:10 Comment(4)
thanks; but xml will not work in my case because of this in my code: StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager( 2, //The number of Columns in the grid LinearLayoutManager.HORIZONTAL);Azriel
Thanks. For normal direction set app:reverseLayout="false".Britisher
For normal direction you can just omit this lineParvenu
For AndroidX , app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"Harte
A
39

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">

Edit

Use reverseLayout property to change scroll direction.

app:reverseLayout="true"

(Thanks to
@Mostafa Imani for this)

Anonymous answered 1/3, 2019 at 9:25 Comment(4)
This requires AndroidX. For support libraries use app:layoutManager="android.support.v7.widget.LinearLayoutManager".Britisher
Yeah so? what is the problem in using androidxHawkinson
but it's still scrolled by default, is there any approach to make it scroll right to left?Tarver
app:reverseLayout="true" is the answerTarver
C
5

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

Covington answered 27/12, 2019 at 1:29 Comment(0)
B
4

Horizontal RecyclerView with imageview and textview

xml file

main.xml

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_marginTop="5dp"
   android:orientation="vertical"
   android:background="#070e94">
<View
    android:background="#787878"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    />
<android.support.v7.widget.RecyclerView
    android:id="@+id/wallet"
    android:background="#070e94"
    android:layout_width="match_parent"
    android:layout_height="100dp"/>

item.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<ImageView
    android:id="@+id/image"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:scaleType="fitXY"
    android:src="@drawable/bus"
    android:layout_gravity="center"/>
<TextView
    android:textColor="#000"
    android:textSize="12sp"
    android:layout_gravity="center"
    android:padding="5dp"
    android:id="@+id/txtView"
    android:textAlignment="center"
    android:hint="Electronics"
    android:layout_width="80dp"
    android:layout_height="wrap_content" />

Java Class

ActivityMaim.java

public class MainActivity extends AppCompatActivity{
private  RecyclerView  horizontal_recycler_view;
private ArrayList<Arraylist> horizontalList;
private CustomAdapter horizontalAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    horizontal_recycler_view= (RecyclerView) findViewById(R.id.horizontal_recycler_view);
    horizontalList = new ArrayList<Arraylist>();
    for (int i = 0; i < MyData.nameArray.length; i++) {
        horizontalList.add(new Arraylist(
                MyData.nameArray[i],
                MyData.drawableArray[i]
        ));
    }
    horizontalAdapter=new CustomAdapter(horizontalList);
    LinearLayoutManager horizontalLayoutManagaer
            = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
    horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
    horizontal_recycler_view.setAdapter(horizontalAdapter);
}}

Adaper Class

CustomAdapter.java

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

private ArrayList<Arraylist> dataSet;

public static class MyViewHolder extends RecyclerView.ViewHolder {

    TextView textViewName;

    ImageView imageViewIcon;

    public MyViewHolder(View itemView) {
        super(itemView);
        this.textViewName = (TextView) itemView.findViewById(R.id.txtView);
        //this.textViewVersion = (TextView) itemView.findViewById(R.id.textViewVersion);
        this.imageViewIcon = (ImageView) itemView.findViewById(R.id.image);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if (getPosition()==0)
                {
                    Toast.makeText(v.getContext(), " On CLick one", Toast.LENGTH_SHORT).show();

                } if (getPosition()==1)
                {
                    Toast.makeText(v.getContext(), " On CLick Two", Toast.LENGTH_SHORT).show();

                } if (getPosition()==2)
                {
                    Toast.makeText(v.getContext(), " On CLick Three", Toast.LENGTH_SHORT).show();

                } if (getPosition()==3)
                {
                    Toast.makeText(v.getContext(), " On CLick Fore", Toast.LENGTH_SHORT).show();

                }

            }
        });
    }
}

public CustomAdapter(ArrayList<Arraylist> data) {
    this.dataSet = data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                       int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_view, parent, false);

    //view.setOnClickListener(MainActivity.myOnClickListener);

    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

    TextView textViewName = holder.textViewName;
   // TextView textViewVersion = holder.textViewVersion;
    ImageView imageView = holder.imageViewIcon;

    textViewName.setText(dataSet.get(listPosition).getName());
    //textViewVersion.setText(dataSet.get(listPosition).getVersion());
    imageView.setImageResource(dataSet.get(listPosition).getImage());
}

@Override
public int getItemCount() {
    return dataSet.size();
}}

Arraylist.java

public class Arraylist{
String name;
int image;

public Arraylist(String name, int image) {
    this.name = name;
    this.image=image;
}
public String getName() {
    return name;
}
public int getImage() {
    return image;
}}

MyData.java

public class MyData {
static String[] nameArray = {"Gas", "Insurance", "Electronics", "Other Services"};
static Integer[] drawableArray = {R.drawable.gas_gas, R.drawable.insurance, R.drawable.electric, R.drawable.services};}
Beaut answered 9/2, 2017 at 7:28 Comment(0)
F
2

In Recycler Layout manager the second parameter is spanCount increase or decrease in span count will change number of elements show on your screen

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2, //The number of Columns in the grid
,GridLayoutManager.HORIZONTAL,false);
                recyclerView.setLayoutManager(mLayoutManager);
Fustian answered 3/4, 2019 at 6:28 Comment(0)
M
1

Try this in fragment :

layoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false);

mRecyclerView.setLayoutManager(layoutManager);
Melina answered 10/3, 2018 at 8:10 Comment(0)
E
1

Just add two lines of code to make orientation of recyclerview as horizontal. So add these lines when Initializing Recyclerview.

  LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);

my_recycler.setLayoutManager(linearLayoutManager);
Executive answered 17/8, 2018 at 9:38 Comment(0)
V
1

This following code is enough

RecyclerView recyclerView;
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,true);

 recyclerView.setLayoutManager(layoutManager);
Verduzco answered 16/10, 2018 at 9:21 Comment(0)
I
1
//in fragment page: 

 recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity(), HORIZONTAL,true));

//this worked for me but before that please import :

implementation 'com.android.support:recyclerview-v7:28.0.0'
Inextensible answered 8/8, 2020 at 6:58 Comment(1)
Can you please add some explaination to your answer?Givens
F
0

Try this

I have tried all above answers it's showing me same vertically recycler view, so I have tried another example.

  1. Initialize the adapter

    private Adapter mAdapter;
    
  2. 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

Fizzy answered 7/2, 2018 at 9:40 Comment(0)
I
0

It's about Persian language problem, Just need to rotate your ListView, GridView, or .... and after that rotate your cell. You can do it in xml android:rotate="360".

Innis answered 28/2, 2019 at 18:13 Comment(0)
A
0
For Horizontal Scroolview in RecyclerView in Kotlin: - We use this Code 
 recycler_upcoming.layoutManager =
            LinearLayoutManager(sContext, LinearLayoutManager.HORIZONTAL, false)
        recycler_upcoming.adapter =
            InboxUpcomingAdapter(this@InboxMessageFragment, arrayList, sContext)

-> sContext mean Ur Context
-> If you pass false then scroll right to left
-> If You pass true then scroll left to right
Abundance answered 25/3, 2021 at 4:45 Comment(0)
W
0

Your XML design here <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvPopularPerson" android:layout_width="match_parent" android:layout_height="215dp"/>

Remember first off all fix recyclerview height like 100dp, 200dp, 300dp etc.

Java Code is here recyclerView= view.findViewById(R.id.rvPopularPerson); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity(), LinearLayoutManager.HORIZONTAL, false));

I think it will help you.

Weaken answered 13/11, 2021 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.