android kotlin recyclerview example

In this tutorial, I am going to tell you about android kotlin recyclerview example. Please check out the previous post regarding recyclerview android with example. In that, I have explained more about what & why recyclerview. In this post, I am only concentrating on android kotlin recyclerview example.

let’s get started into kotlin recyclerview example.

setup recyclerview

lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        recyclerView = findViewById(R.id.recyclerview)
        recyclerView.layoutManager = LinearLayoutManager(this)
}

Recyclerview Data Model

data class Movie(var name: String,var url: String) {
}

Create Recyclerview Adapter

class MovieListAdapter(var context: Context) : RecyclerView.Adapter<MovieListAdapter.MovieViewHolder>() {

    var movieList: MutableList<Movie> = mutableListOf()

    override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
        val movie = movieList.get(position)
        holder.tvName?.text = movie.name
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.movie_list_item,parent,false)
      return MovieViewHolder(view)
    }

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

    fun setMovieLists(list: List<Movie>){
        movieList.addAll(list)
        notifyDataSetChanged()
    }


    class MovieViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {

        var tvName: TextView = itemView!!.findViewById(R.id.tv_name);
    }
}

Finally, set Adapter to Recyclerview and add values into recyclerview.

movieListAdapter = MovieListAdapter(this)
recyclerView.adapter = movieListAdapter

var movieList : MutableList<Movie> = mutableListOf()
movieList.add(Movie("Movie 1","link 1"))
movieList.add(Movie("Movie 2","link 2"))
movieList.add(Movie("Movie 3","link 3"))
movieList.add(Movie("Movie 4","link 4"))
movieListAdapter.setMovieLists(movieList)

MainActivity.kt

class MainActivity : AppCompatActivity() {

    lateinit var recyclerView: RecyclerView

    lateinit var movieListAdapter: MovieListAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        recyclerView = findViewById(R.id.recyclerview)
        recyclerView.layoutManager = LinearLayoutManager(this)
        movieListAdapter = MovieListAdapter(this)
        recyclerView.adapter = movieListAdapter

        var movieList : MutableList<Movie> = mutableListOf()
        movieList.add(Movie("Movie 1","link 1"))
        movieList.add(Movie("Movie 2","link 2"))
        movieList.add(Movie("Movie 3","link 3"))
        movieList.add(Movie("Movie 4","link 4"))
        movieListAdapter.setMovieLists(movieList)

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top
%d bloggers like this: