---
# System prepended metadata

title: Customize recyclerView

---

# Customize recyclerView
1. Customize a list layout name 'list_view.xml'
```xml=
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/item_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="title"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/item_detail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="detail"
            android:textStyle="italic" />

    </LinearLayout>

    <Button
        android:id="@+id/item_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete"/>

</LinearLayout>
```

2. Modify adapter class
```kotlin=
class MyAdapter(private val mData: ArrayList<MyItem>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view){

        val title: TextView = view.findViewById(R.id.item_title)

        val detail: TextView = view.findViewById(R.id.item_detail)

        val deleteBtn: Button = view.findViewById(R.id.item_delete)
    }

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

        val view = LayoutInflater.from(parent.context).inflate(R.layout.list_view,parent,false)

        return MyViewHolder(view)
    }

    override fun getItemCount(): Int{

        return mData.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        val myViewHolder = holder as MyViewHolder

        val item = mData[position]

        myViewHolder.title.text = item.title

        myViewHolder.detail.text = item.detail

        myViewHolder.deleteBtn.setOnClickListener {

            deleteItem(holder.adapterPosition)
        }
    }

    fun deleteItem(index: Int){

        if(index >=0 && index < mData.size){

            mData.removeAt(index)

            notifyItemRemoved(index)
        }
    }

    fun addItem(item:MyItem){

        val index = mData.size

        mData.add(index,item)

        notifyItemInserted(index)
    }
}

class MyItem(val title:String,val detail:String)
```

3. Using
```kotlin=
val data = ArrayList<MyItem>().apply {

    add(MyItem("Reading","Android study"))
    add(MyItem("Workout","Full body training"))
    add(MyItem("Shopping","Buy some broccoli"))
    add(MyItem("Writing","Write some article"))
    add(MyItem("Others","Do anything"))
}

val adpater = MyAdapter(data)

val recycleView = view.findViewById<RecyclerView>(R.id.my_recycle_view)

recycleView.layoutManager = LinearLayoutManager(context)

recycleView.adapter = adpater

fab.setOnClickListener {

    adpater.addItem(MyItem("New Item","$#%@$%@$%"))
}
```

4. Result
![](https://i.imgur.com/zNAjKo7.png)
