Giter VIP home page Giter VIP logo

generic-recyclerview-adapter's Introduction

Generic-RecyclerView-Adapter

Introduction

Generic RecyclerView Adapter is a generic implementation of RecyclerView Adapter. By using this we don't need to create multiple adapters.


Usage

  1. Create adapter object
lateinit var testAdapter: GenericAdapter<TestModel>
  1. Instantiate adapter object (pass context, layout, list and Binder object in the constructor).

Set your layout data within bind method

testAdapter = GenericAdapter(this, R.layout.row_test, animalList, object :  GenericAdapter.Binder<TestModel> {
            override fun bind(holder: GenericAdapter<TestModel>.ViewHolder, position: Int) {
                holder.itemView.findViewById<TextView>(R.id.test_tv).setText((testAdapter.getItem(position) as TestModel).text)

                holder.itemView.setOnClickListener {
                    Toast.makeText(this@MainActivity, "You clicked " + (testAdapter.getItem(position) as TestModel).text + " on row number " + position, Toast.LENGTH_SHORT).show()
                }
            }
        })
  1. Implementation

MainActivity.kt

package matrixsystems.genericrecyclerviewadapter

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.LinearLayoutManager


class MainActivity : AppCompatActivity() {

    lateinit var recyclerView : RecyclerView

    lateinit var testAdapter: GenericAdapter<TestModel>

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


        val animalList = mutableListOf<TestModel>()
        animalList.add(TestModel("Horse"))
        animalList.add(TestModel("Cow"))
        animalList.add(TestModel("Camel"))
        animalList.add(TestModel("Sheep"))
        animalList.add(TestModel("Goat"))

        recyclerView = findViewById(R.id.recycler_view) as RecyclerView
        recyclerView.layoutManager = LinearLayoutManager(this)
        testAdapter = GenericAdapter(this, R.layout.row_test, animalList, object :  GenericAdapter.Binder<TestModel> {
            override fun bind(holder: GenericAdapter<TestModel>.ViewHolder, position: Int) {
                holder.itemView.findViewById<TextView>(R.id.test_tv).setText((testAdapter.getItem(position) as TestModel).text)

                holder.itemView.setOnClickListener {
                    Toast.makeText(this@MainActivity, "You clicked " + (testAdapter.getItem(position) as TestModel).text + " on row number " + position, Toast.LENGTH_SHORT).show()
                }
            }
        })
        recyclerView.adapter = testAdapter
    }

}

generic-recyclerview-adapter's People

Contributors

shahbazhashmi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.