RecyclerView及适配器功能的用法
<androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rv1"app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"android:orientation="horizontal"android:layout_marginHorizontal="20dp"android:layout_marginTop="20dp"android:layout_width="match_parent"android:layout_height="wrap_content"/>
RecyclerView的适配器
定义一个函数
private fun initRv2(){val mutableListOf = mutableListOf<String>("Simon","Dion","Sam","Chris","Simon","Dion","Sam","Chris",)binding.rv1.adapter = RvA(mutableListOf, R.layout.item02) { itemData, position, view ->Item02Binding.bind(view).apply {when (itemData) {"Simon" -> image.setImageResource(R.drawable.y1)"Dion" -> image.setImageResource(R.drawable.y2)"Sam" -> image.setImageResource(R.drawable.v1)"Chris" -> image.setImageResource(R.drawable.v2)}text.text = itemDatadel.visibility = View.GONEimage.setOnClickListener {del.visibility = View.VISIBLEdel.setOnClickListener {mutableListOf.removeAt(position)dataList = mutableListOfnotifyItemRemoved(position)notifyItemChanged(position, mutableListOf.size)}}}}
}
写出recycleview的子项 item02
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_marginVertical="10dp"android:layout_marginHorizontal="20dp"android:layout_height="wrap_content"xmlns:app="http://schemas.android.com/apk/res-auto"><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="70dp"android:layout_height="70dp"><androidx.constraintlayout.utils.widget.ImageFilterViewandroid:layout_width="match_parent"android:src="@drawable/source13"android:scaleType="centerCrop"app:round="10dp"android:id="@+id/image"android:layout_height="match_parent"/><androidx.constraintlayout.utils.widget.ImageFilterViewandroid:layout_width="match_parent"android:src="@drawable/del"android:padding="20dp"app:round="10dp"android:id="@+id/del"android:scaleType="centerCrop"android:background="@color/black_2a"android:layout_height="match_parent"/></androidx.constraintlayout.widget.ConstraintLayout><TextViewandroid:text="你问问阿萨德"android:id="@+id/text"android:textSize="13dp"android:layout_marginTop="10dp"android:gravity="center"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>