Blog purpose for android basic example for android app developer. any query please my contact

Monday, 22 February 2021

kotlin recyclerview example

kotlin recyclerview example

In this Article Today Learn kotlin recycler view example. follow the full Source code kotlin recycler view example.

1.Recylerview_Actvity.kt:

package com.akash.kotlinlistview.recylerview

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle

import androidx.recyclerview.widget.DefaultItemAnimator

import androidx.recyclerview.widget.LinearLayoutManager

import androidx.recyclerview.widget.RecyclerView

import com.akash.kotlinlistview.R


class Recylerview_Actvity : AppCompatActivity() {

    private val movieList = ArrayList<MovieModel>()

    private lateinit var moviesAdapter: AdapterM

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_recylerview__actvity)

        title = "KotlinApp"

        val recyclerView: RecyclerView = findViewById(R.id.recyclerView)

        moviesAdapter = AdapterM(movieList)

        val layoutManager = LinearLayoutManager(applicationContext)

        recyclerView.layoutManager = layoutManager

        recyclerView.itemAnimator = DefaultItemAnimator()

        recyclerView.adapter = moviesAdapter

        prepareMovieData()

    }

    private fun prepareMovieData() {

        var movie = MovieModel("1", "Akash", "984571")

        movieList.add(movie)

        movie = MovieModel("2", "Jay", "998576")

        movieList.add(movie)

        movie = MovieModel("3", "Prakash", "889617")

        movieList.add(movie)

        movie = MovieModel("4", "Vijay", "478954")

        movieList.add(movie)

        movie = MovieModel("5", "Ketan", "98225")

        movieList.add(movie)

        movie = MovieModel("6", "Yogesh", "47856")

        movieList.add(movie)

        movie = MovieModel("7", "Ganesh", "897562")


        movieList.add(movie)

        moviesAdapter.notifyDataSetChanged()

    }

}



2.MovieModel.kt:

package com.akash.kotlinlistview.recylerview

class MovieModel(emp_id: String?, name: String?, mobile: String?) {
    private var emp_id: String
    private var name: String
    private var mobile: String
    init {
        this.emp_id = emp_id!!
        this.name = name!!
        this.mobile = mobile!!
    }
    fun getemp_id(): String? {
        return emp_id
    }
    fun setemp_id(emp_id: String?) {
        this.emp_id = emp_id!!
    }
    fun getname(): String? {
        return name
    }
    fun setname(name: String?) {
        this.name = name!!
    }
    fun getmobile(): String? {
        return mobile
    }
    fun setmobile(mobile: String?) {
        this.mobile = mobile!!
    }
}

3.AdapterM.kt:

package com.akash.kotlinlistview.recylerview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView
import com.akash.kotlinlistview.R

internal class AdapterM(private var moviesList: List<MovieModel>) :
    RecyclerView.Adapter<AdapterM.MyViewHolder>() {
    internal inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var title: TextView = view.findViewById(R.id.title)
        var year: TextView = view.findViewById(R.id.year)
        var genre: TextView = view.findViewById(R.id.genre)
    }
    @NonNull
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.m_list, parent, false)
        return MyViewHolder(itemView)
    }
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val movie = moviesList[position]
        holder.title.text = movie.getemp_id()
        holder.genre.text = movie.getname()
        holder.year.text = movie.getmobile()
    }
    override fun getItemCount(): Int {
        return moviesList.size
    }
}

4.activity_recylerview__actvity.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="vertical">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

5.m_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_margin="8dp"
    app:cardBackgroundColor="@android:color/holo_red_dark">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp">
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_toStartOf="@+id/year"
            android:textColor="@android:color/white"
            android:textSize="16sp"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/year"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:textColor="@android:color/white" />
        <TextView
            android:id="@+id/genre"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:textColor="@android:color/white" />
    </RelativeLayout>
</androidx.cardview.widget.CardView>

6.AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akash.kotlinlistview">

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">y
        <activity android:name=".recylerview.Recylerview_Actvity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

7.Output:

kotlin recyclerview example




No comments:

Post a Comment