Category

RecyclerView

Category

horizontal recyclerview android example
Do you want to make Horizontal RecyclerView?

Before jumping into tutorial, I would recommend reading RecyclerView In Android : The Ultimate Guide.

In this post, I will teach you how to make Horizontal RecyclerView In Android with 4 ideas. In the end, we will create a Horizontal RecyclerView project too.

Okay… Let’s start.

RecyclerView uses LayoutManager class to arrange its items, so below we are going to use the LayoutManager class to make it work.

1. Horizontal RecyclerView Using LinearLayoutManager

This is the first and most commonly used option to make Horizontal RecyclerView. LinearLayoutManager provides a constructor that we can change the orientation of RecyclerView.

LinearLayoutManager(Context context, int orientation, boolean reverseLayout)

  • Here, we use “this” because RecyclerView uses in MainActivity. If you are using RecyclerView in fragment, you can use getActivity()
  • false : Do not reverse the layout.

2.Horizontal RecyclerView Using app:LayoutManager attribute

  • app:layoutManager=”androidx.recyclerview.widget.LinearLayoutManager”: sets LayoutManger for RecyclerView.
  • android:orientation=”horizontal” : Render RecyclerView in Horizontal.
  • For support library users, Use this app:layoutManager=”android.support.v7.widget.LinearLayoutManager”.

3. Horizontal RecyclerView Using GridLayoutManager

In this part, we use GridLayoutManager for creating horizontal RecyclerView. Use the below one.

GridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout)

spanCount: Number of rows or columns based on orientation.


You can use XML way, just like below:

  • For support library users, app:layoutManager=”android.support.v7.widget.GridLayoutManager”

4. Horizontal Recyclerview Using StaggeredGridLayoutManager

In this section we can use StaggeredGridLayoutManager constructor, shown below:

StaggeredGridLayoutManager(int spanCount, int orientation)


In XML way

  • For support library users, Use app:layoutManager=”android.support.v7.widget.StaggeredGridLayoutManager”/li>

.

Horizontal RecyclerView In Android – Main Example

Let’s create a horizontal RecyclerView…

Just like below

Horizontal RecyclerView Android Example

Open Android Studio and start a new Android Studio project.

Application name : Horizontal RecyclerView Android Example.

Company domain: androidride.com

Check to include Kotlin support.


Select minimum SDK:API 15 – Android 4.0.3 (Ice Cream Sandwich) and click Next.

Next dialog, Select Empty Activity and click Next.

Activity Name: MainActivity
Check Generate layout file

Layout Name: activity_main

If you are using the latest Android Studio versions, there will be a CheckBox with the text “Use androidx.* artifacts”. Make sure that’s checked, which creates our project with androidx library.
Click Finish.

build.gradle

colors.xml

strings.xml

styles.xml

activity_main.xml

MainActivity.kt

item.xml

RecyclerViewAdapter.kt


Let’s run it and please share it your friends, family and co-workers.