Android Horizontal RecyclerView scroll Direction

horizontal recyclerview with cardview android
horizontal recyclerview android kotlin
android recyclerview horizontal scroll animation
horizontal grid recyclerview android
horizontal recyclerview android github
multiple horizontal recyclerview android
horizontal recyclerview with indicator android
auto scroll horizontal recyclerview android

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?

My Code:

StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(
                        2, //The number of Columns in the grid
                        LinearLayoutManager.HORIZONTAL);

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.

Android Horizontal RecyclerView scroll Direction, 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  Android Horizontal RecyclerView scroll Direction then How can I change the RecyclerView scroll direction like in 4 Ways To Create Horizontal RecyclerView In

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

Android Horizontal RecyclerView scroll Direction, 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  Questions: I need to detect the start/end and direction of scroll in a recyclerview. The scroll listener has two methods: onScrolled() and onScrollStateChanged(). The first method is called after the scroll is started (indeed is called onScrolled() and not onScrolling()). The second method gives information about the state but I don’t have

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

Fixing RecyclerView nested scrolling in opposite direction, RecyclerView is one of the main building blocks of Android user Notice that attempting to scroll the nested horizontal RecyclerView s often  RecyclerView dependencies. implementation 'com.android.support:recyclerview-v7:26.1.0' After the recyclerview is initialized, a scroll listener is added to it to listen to scroll event When a scroll event happens, first check it is not currently loading more items

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};}

LinearLayoutManager, LinearLayoutManager(Context context, int orientation, boolean reverseLayout) Checks if RecyclerView is in the middle of a layout or scroll and throws an IllegalStateException if it is. Query if horizontal scrolling is currently supported. When the target scroll position is not a child of the RecyclerView, this method calculates a direction vector towards that child and triggers a smooth scroll. Inherited methods From class android.support.v7.widget.RecyclerView.SmoothScroller

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);

ScrollView, For horizontal scrolling, use HorizontalScrollView instead. Never add a RecyclerView or ListView to a scroll view. Doing so results in poor user interface​  In one of our apps most screens are composed of a small set of reusable UI components, glued together vertically using a RecyclerView. Our designer had a simple request for one of these screens: Add a fixed-height multi-line text input. Obviously that input must scroll vertically when the text becomes too long.

RecyclerView.LayoutManager, Checks if RecyclerView is in the middle of a layout or scroll and throws an Called when searching for a focusable view in the given direction has failed for the For example, if this LayoutManager represents a horizontally scrolling list within  Continuing with Support Library 26 features (if you missed the one with downloadable fonts), a much awaited feature was enabled : fast scrolling for RecyclerView.. Of all the great advantages RecyclerView has over ListView, one feature that I miss a lot is fast scrolling where you could drag a thumb drawable and scroll around the list.

GridLayoutManager, GridLayoutManager(Context context, int spanCount, int orientation, boolean in the given direction has failed for the current content of the RecyclerView. Scroll horizontally by dx pixels in screen coordinates and return the distance traveled. Project on how to Create Horizontal Scrolling RecyclerView in Android. Go ahead and create a new android project from File > New Project. Fill in all the necessary details, when prompted to select an activity, select Basic Activity and proceed. First create items.xml layout from layout > new > Layout Resources File.

Create a horizontal list, Use the standard ListView constructor, passing in a horizontal scrollDirection , which overrides the default vertical direction. content_copy. ListView( // This next​  public void onScrolled (RecyclerView recyclerView, int dx, int dy) Callback method to be invoked when the RecyclerView has been scrolled. This will be called after the scroll has completed. This callback will also be called if visible item range changes after a layout calculation. In that case, dx and dy will be 0.

Comments
  • 4 Ways - Horizontal RecyclerView In Android
  • Yes, depending upon the flag that is passing in the constructor the scroll will change. Thanks a lot!
  • 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);
  • Thanks. For normal direction set app:reverseLayout="false".
  • For normal direction you can just omit this line
  • This requires AndroidX. For support libraries use app:layoutManager="android.support.v7.widget.LinearLayoutManager".
  • Yeah so? what is the problem in using androidx