How to use RecyclerView to create ViewPager2 in Kotlin

In this android kotlin source code example, we are going to use RecyclerView to create ViewPager2 in Kotlin.

You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel.

Below is a step by step source code to use RecyclerView to create ViewPager2 in Kotlin.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewPager.ViewPagerActivity9">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.bluapp.kotlinview.R

class ViewPagerActivity9 : AppCompatActivity() {
    private var viewPager: ViewPager2? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_pager9)
        viewPager = findViewById(R.id.viewpager) as ViewPager2
        viewPager!!.adapter = MyAdapter()
    }

    private inner class MyAdapter : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
        private val colorlists = intArrayOf(
            android.R.color.black,
            android.R.color.holo_green_light,
            android.R.color.holo_purple,
            android.R.color.holo_blue_dark
        )

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder = MyViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.viewpager_item, parent, false))

        override fun getItemCount(): Int {
            return colorlists.size
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            holder.itemPosition.text = "Position $position"
            holder.container.setBackgroundResource(colorlists[position])
        }

        inner class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
            internal var itemPosition: AppCompatTextView
            internal var container: ConstraintLayout
            init {
                itemPosition = itemView.findViewById(R.id.itemPosition)
                container = itemView.findViewById(R.id.container)
            }
        }
    }
}

viewpager_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/itemPosition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.