RecyclerView及适配器功能的用法
<androidx.recyclerview.widget.RecyclerView
android: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 = itemData
del.visibility = View.GONE
image.setOnClickListener {
del.visibility = View.VISIBLE
del.setOnClickListener {
mutableListOf.removeAt(position)
dataList = mutableListOf
notifyItemRemoved(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.ConstraintLayout
android:layout_width="70dp"
android:layout_height="70dp">
<androidx.constraintlayout.utils.widget.ImageFilterView
android: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.ImageFilterView
android: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>
<TextView
android: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>