Open top menu

In this tutorial explain how to bind RecyclerView via kotlin.


RecyclerAdapter.kt


class RecyclerAdapter(val items: ArrayList, val context :Context,  val btnlistener: BtnClickListener) :RecyclerView.Adapter() {

    companion object {
        var mClickListener: BtnClickListener? = null    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.animal_list_item, parent, false))
    }

    override fun getItemCount(): Int {

        return items.size    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        mClickListener = btnlistener        holder?.tvAnimalType.text = items.get(position)
        holder?.tvAnimalType.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                if (mClickListener != null)
                    mClickListener?.onBtnClick(position,holder?.tvAnimalType.text.toString())
            }
        })
    }


    class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
        val tvAnimalType = view.tv_animal_type
    }

    open interface BtnClickListener {
        fun onBtnClick(position: Int,string: String)
    }
}


layout_row.xml

xml version="1.0" encoding="utf-8";
xmlns:android="http://schemas.android.com/apk/res/android"              
android:layout_width="match_parent"              
android:layout_height="wrap_content">
    
TextView android:id="@+id/tv_animal_type"            
android:layout_width="match_parent"            
android:layout_height="wrap_content"            
android:textSize="20sp"            
android:padding="20dp" 
</LinearLayout >



 
MainActivity .kt

class MainActivity : AppCompatActivity(), RecyclerAdapter.BtnClickListener {

    override fun onBtnClick(position: Int, string: String) {
        Toast.makeText(this, "hello" + position + string, Toast.LENGTH_LONG).show()
    }

    val animals: ArrayList = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        addAnimal();
        rv_animal_list.layoutManager = LinearLayoutManager(this)
        rv_animal_list.adapter = RecyclerAdapter(animals, this, this)
    }

    fun addAnimal() {
        animals.add("dog")
        animals.add("cat")
        animals.add("owl")
        animals.add("cheetah")
        animals.add("raccoon")
        animals.add("bird")
        animals.add("snake")
        animals.add("lizard")
        animals.add("hamster")
        animals.add("bear")
        animals.add("lion")
        animals.add("tiger")
        animals.add("horse")
        animals.add("frog")
        animals.add("fish")
        animals.add("shark")

    }

}

activity_main.xml

xml version="1.0" encoding="utf-8"
?><android.support.design.widget.CoordinatorLayout        
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"        t
ools:context=".activites.OneActivity">

   
 <android.support.design.widget.AppBarLayout            
android:layout_height="wrap_content"            
android:layout_width="match_parent"            
android:theme="@style/AppTheme.AppBarOverlay">

        
<android.support.v7.widget.Toolbar               
 android:id="@+id/toolbar"                
android:layout_width="match_parent"                
android:layout_height="?attr/actionBarSize"                
android:background="?attr/colorPrimary"                
app:popupTheme="@style/AppTheme.PopupOverlay"/>
 </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_one"/>

    <android.support.design.widget.FloatingActionButton            
android:id="@+id/fab"            
android:layout_width="wrap_content"            
android:layout_height="wrap_content"           
 android:layout_gravity="bottom|end"            
android:layout_margin="@dimen/fab_margin"            
app:srcCompat="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>


0 comments