How to Create Android Horizontal Scrolling RecyclerView Download

A good example of Android’s horizontal scrolling is RecyclerView, which can be found in the Play Store app. Horizontal scrolling RecyclerView is typically used in scripts when products or data are loaded by category or perhaps when trying to make some fancy design in your application. Today you will learn how to easily implement horizontal RecyclelerView scrolling in Android.

Download test application

How to create android horizontal scroller view

Read also, Android Infinite Scrolling RecyclerView

If you have Basic introduction to ReyclerViewthen this lesson will just be walking.

To achieve a horizontal scrolling list in the recyclerView program, simply add RecyclerView orientation your parameter LayoutManager as shown in the code below.

RecyclerView recyclerView = findViewById (id);
recyclerView.setLayoutManager (new LinearLayoutManager (this, LinearLayoutManager.HORIZONTAL, false));

This alone will make your RecyclerView lists for horizontal scrolling. I found problems adding recyclerView.setHasFixedSize (true);ignore recyclerView.setHasFixedSize (true); for now, but if it works for you, just go with that.

Read also, How to hide the floating android button when scrolling RecyclerView

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 required data, when prompted to select an action, select Primary activity and continue.

First create items.xml layout of layout> new> layout resource fileThis file will represent a single row element in our reyclerView, but since we use the horizontal scrolling of the reyclerView, we can also call it a single column element.

Add the following code to your content_main.xml file, you can also replace ConstraintLayout with any other layout, for example RelativeLayout.

We are going to create two classes for our ReyclerView. The first class is a model class that will contain an image and text. The second class will be our adapter ReyclerView. So add the following code to your model class.

the hack.patchversion.patchversiontutorials package; public class HackSmileModelClass { private image int; private string name; public HackSmileModelClass (int image, String title) { this.image = image; this.title = title; } public int getImage () { return image; } public String getTitle () { return title; } }

Add the following code to your ReyclerView adapter class.
CustomAdapter.java

the hack.patchversion.patchversiontutorials package;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

Public class CustomAdapter extends RecyclerView.Adapter {

context of private context;
private ArrayList Items;

public CustomAdapter (Context context, ArrayList Items) {
this.context = context;
this.items = items;
}

@NonNull
@Override
public CustomViewHolder onCreateViewHolder (parent @NonNull ViewGroup, int viewType) {
return a new CustomViewHolder (LayoutInflater.from (context) .inflate (R.layout.custom_item_layout, parent, false));
}

@Override
public void onBindViewHolder (@NonNull CustomViewHolder, position int) {
holder.itemTitle.setText (items.get (position) .getTitle ());
holder.itemImage.setImageResource (items.get (position) .getImage ());
}

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

public class CustomViewHolder extends RecyclerView.ViewHolder {

private ImageView itemImage;
Private TextView itemTitle;

public CustomViewHolder (View View) {
super (view);
itemImage = view.findViewById (R.id.item_image);
itemTitle = view.findViewById (R.id.item_title);
}
}
}

Now it’s time to implement a horizontal scrolling list in RecyclerView. Add the following code to your MainActivity.java class.

the hack.patchversion.patchversiontutorials package;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate (BundlevedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
Toolbar toolbar = findViewById (R.id.toolbar);
setSupportActionBar (toolbar);


ArrayList items = new ArrayList <> ();
CustomAdapter Adapter = new CustomAdapter (this, elements);

RecyclerView recyclerView = findViewById (R.id.recycler_view);
recyclerView.setLayoutManager (new LinearLayoutManager (this, LinearLayoutManager.HORIZONTAL, false));

recyclerView.setAdapter (adapter);

// let's create 10 random items

for (int i = 0; i <10; i ++) {
items.add (new HackSmileModelClass (R.drawable.Picture1, "Title" + i));
adapter.notifyDataSetChanged ();
}

}


}

Download: Link-1 |Link-2 |Link-3

Download

Leave a Comment

Your email address will not be published. Required fields are marked *