Recyclerview horizontal kotlin

Recyclerview horizontal kotlin

1. Overview

In this article, we will learn to implement Recyclerview with the horizontal orientation in Kotlin.

2. Recyclerview horizontal kotlin

To implement a horizontal recycler view, create an instance of LinearLayoutManager with orientation HORIZONTAL and assign it to the recycler view.

The below code would make the Recyclerview to show the elements in HORIZONTAL orientation.

  mRecyclerView?.layoutManager = LinearLayoutManager(
            this,
             LinearLayoutManager.HORIZONTAL, false)

If you want to get full sample code for horizontal recycler view, then continue reading the section.

The below MainActivity initializes the RecyclerView and uses the horizontal linear layout manager.

package com.tedblob.kotlin.horizontal.recyclerview
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout.HORIZONTAL
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
    private var mAdapter: RecyclerAdapter? = null
    private var mRecyclerView: RecyclerView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mRecyclerView = findViewById(R.id.recyclerView)
        mRecyclerView?.layoutManager = LinearLayoutManager(
            this,
             LinearLayoutManager.HORIZONTAL, false)
        val dataset = arrayOfNulls<String>(50)
        for (i in dataset.indices) {
            dataset[i] = "item$i"
        }
        mAdapter = RecyclerAdapter(dataset, this)
        mRecyclerView?.adapter = mAdapter
    }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

We don’t create any custom view for recycler view elements in this example. Instead, we use the Android provied layout simple_list_item_1 as the adapter view for displaying the elements.

package com.tedblob.kotlin.horizontal.recyclerview
import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import android.widget.TextView
import com.tedblob.kotlin.horizontal.recyclerview.R
import android.widget.Toast
import android.view.ViewGroup
import android.view.LayoutInflater
import android.view.View
class RecyclerAdapter(private val mDataset: Array<String?>, private val mContext: Context) :
    RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var mTextView: TextView = itemView.findViewById<View>(android.R.id.text1) as TextView
    }
    override fun getItemCount(): Int {
        return mDataset.size
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.mTextView.text = mDataset[position]
    }
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val view: View = LayoutInflater.from(parent.context).inflate(
            android.R.layout.simple_list_item_1, parent, false
        )
        return ViewHolder(view)
    }
}

3. Conclusion

To sum up, we learned how to show Recyclerview contents in horizontal orientation.

Leave a Comment