SoFunction
Updated on 2025-03-01

Android development uses RecyclerView to add click event instances for detailed explanation

introduction

RecyclerView was introduced as a component of support-v7 in Android 5.0, effectively replacing the original ListView and other list components.

Implemented on useView code decoupling, with powerful items in functionReuse mechanism, and provides default LayoutManangers to handle multiple layouts. This article introduces it to RecyclerViewBasic useandAdd a click event

Add toRecyclerViewrely

implementation ':recyclerview:1.1.0'

1. Basic use of RecyclerView

1. Add Adapter Adapter

class NewsAdapter(private val newsList: List<NewsInfo>, ): <>() {
    class ViewHolder(itemView: View) : (itemView) {
        // ViewHolder
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = ().inflate(.item_layout, parent, false)
        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // Bind data    }
    override fun getItemCount(): Int {
        return 
    }
}

2. Create the item_layout.xml file for each item in the list

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:orientation="vertical"
    android:layout_marginHorizontal="10dp"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:text="title"
        android:textSize="16sp"
        android:textColor="#000000" />
    <ImageView
        android:
        android:layout_width="match_parent"
        android:layout_height="200dp" />
    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:layout_marginTop="10dp"
        android:background="#d3d3d3" />
</LinearLayout>

3. Use in activity

    // Set up the LayoutManager, here we use LinearLayoutManager linear layout as an example     = LinearLayoutManager()
    // Create an adapter    val newsAdapter = NewsAdapter(newsList)
    // Set up the adapter     = newsAdapter

2. Detailed steps for RecyclerView click event

1. Create a new interface in the Adapter class corresponding to RecyclerView

    //Create the OnItemClickListener interface    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

2. Create the setOnItemClickListener method in the Adapter class

    //Declare an mItemClickListener interface    private var mOnItemClickListener: OnItemClickListener? = null
    //Add setOnItemClickListener method    fun setOnItemClickListener(listener: OnItemClickListener?) {
        mOnItemClickListener = listener
    }

3. Set callbacks for each item in the onBindViewHolder of the Adapter class

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        ......
        //Set callbacks for each Item's click event         {
            mOnItemClickListener?.onItemClick(position)
        }
    }

4. Add click event in the activity corresponding to RecyclerView

    val adapter = YourAdapter(YourData)  //adapter    (object :  {
        override fun onItemClick(position: Int) {
            (this@YourActivity, "You clicked No.${position} item.", Toast.LENGTH_SHORT).show()
        }
    })

Summarize

  • existRecyclerViewCorrespondingAdapterCreate a new interface in the class
  • existAdapterCreated in the classsetOnItemClickListenermethod
  • existAdapterClassiconBindViewHolderGive eachitemSet callback
  • existRecyclerViewCorrespondingActivityAdd click event

The above is the detailed explanation of the detailed example of Android development using RecyclerView to add click event instances. For more information about adding click events to Android RecyclerView, please follow my other related articles!