Kotlin RecyclerView Example

Source code of RecyclerView Adapter

 

Step 1 :- Create new kotlin class and named as KotlinRecyclerAdapter.kt and paste the following.  

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.support.v7.widget.RecyclerView


/** * Created by ravi on 11/7/17. */

class KotlinRecyclerAdapter(private val moviesList: List<Data>) : RecyclerView.Adapter<KotlinRecyclerAdapter.MyViewHolder>() {

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var title: TextView = view.findViewById<TextView>(R.id.title)
        var year: TextView = view.findViewById<TextView>(R.id.year)
        var genre: TextView = view.findViewById<TextView>(R.id.genre)

    }

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

        return MyViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val Movies = moviesList[position]
        holder.title.text = Movies.title        holder.genre.text = Movies.genre        holder.year.text = Movies.year    }

    override fun getItemCount(): Int {
        return moviesList.size    }
}
 
 
 
Step 2 :-Create new kotlin class and named as Data.kt and paste the following.  


/** * Created by ravi on 11/7/17. */
class Data {
    var title: String = ""    var genre: String = ""    var year: String = ""
    constructor() {}
    constructor(title: String, genre: String, year: String) {
        this.title = title
        this.genre = genre
        this.year = year
    }
}
 
 
 
 
Step 3 :-Open MainActivity.kt class and paste the following.  
 
package com.example.ravi.myapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import java.util.ArrayList
import android.support.v7.widget.RecyclerView


class MainActivity : AppCompatActivity() {


    private var recyclerView: RecyclerView? = null

    private var mAdapter: KotlinRecyclerAdapter? = null
    private var movieList = ArrayList<Data>()
 

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerView = findViewById<RecyclerView>(R.id.recyclerView) as RecyclerView

        mAdapter = KotlinRecyclerAdapter(movieList)
        val mLayoutManager = LinearLayoutManager(applicationContext)
        recyclerView!!.layoutManager = mLayoutManager
        recyclerView!!.itemAnimator = DefaultItemAnimator()
        recyclerView!!.adapter = mAdapter
        var movie = Data("Mashan", "Drama & Romance", "2015")
        movieList.add(movie)

        movie = Data("Kadwi Hawa", "Drama & Emotions ", "2017")
        movieList.add(movie)


        movie = Data("Padmawati", "Drama & Hostorical", "2017")
        movieList.add(movie)

        mAdapter!!.notifyDataSetChanged()



    }
} 

 
 
 Step 4 :- Now RUN the Application.
 

Comments

Popular posts from this blog

Using Kotlin JSON Parsing with Listview

Android GPS Location tracking User's current location.

The Android Network connection Operations