用kotlin写recycleView,每个item包含一个勾选框、姓名、身份证、电话等信息,底部有添加和删除按钮。点击添加按钮,进入添加界面,添加用户信息item;勾选recycleVIew的勾选框,点击删除按钮,删除此个item信息
时间: 2024-03-15 16:44:44 浏览: 13
好的,以下是Kotlin实现RecyclerView并包含勾选框、姓名、身份证、电话等信息、添加和删除按钮的代码示例:
1. 创建RecyclerView的item布局文件,例如item_layout.xml:
```
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="item"
type="com.example.UserItem" />
<variable
name="clickListener"
type="com.example.ItemClickListener" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@{item.checked}"
android:onClick="@{() -> clickListener.onCheckClicked(item)}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{item.name}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{item.idCard}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{item.phone}" />
</LinearLayout>
</layout>
```
2. 创建RecyclerView的适配器类,例如UserAdapter.kt:
```
class UserAdapter(private val items: MutableList<UserItem>, private val clickListener: ItemClickListener) : RecyclerView.Adapter<UserAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ItemLayoutBinding.inflate(inflater, parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(items[position], clickListener)
}
override fun getItemCount(): Int {
return items.size
}
inner class ViewHolder(private val binding: ItemLayoutBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: UserItem, clickListener: ItemClickListener) {
binding.item = item
binding.clickListener = clickListener
binding.executePendingBindings()
}
}
}
```
3. 在布局文件中添加RecyclerView、添加按钮和删除按钮,例如activity_main.xml:
```
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="com.example.MainViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:items="@{viewModel.items}"
app:clickListener="@{viewModel.clickListener}" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:onClick="@{() -> viewModel.addItem()}" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:onClick="@{() -> viewModel.deleteItems()}" />
</LinearLayout>
</LinearLayout>
</layout>
```
注意,这里使用了自定义属性`app:items`和`app:clickListener`来绑定RecyclerView的数据和点击事件。
4. 在MainActivity中实现添加和删除用户信息,例如MainActivity.kt:
```
class MainActivity : AppCompatActivity(), ItemClickListener {
private lateinit var binding: ActivityMainBinding
private lateinit var adapter: UserAdapter
private val viewModel by viewModels<MainViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.lifecycleOwner = this
adapter = UserAdapter(viewModel.items, this)
binding.recyclerView.adapter = adapter
}
override fun onCheckClicked(item: UserItem) {
viewModel.checkItem(item)
}
override fun onDeleteClicked(item: UserItem) {
viewModel.deleteItem(item)
}
}
```
注意,这里通过实现`ItemClickListener`接口来处理勾选框和删除按钮的点击事件,并且使用了`by viewModels`来创建`MainViewModel`实例。
5. 创建ViewModel类,例如MainViewModel.kt:
```
class MainViewModel : ViewModel() {
val items = MutableLiveData<MutableList<UserItem>>(mutableListOf())
val clickListener = this
fun addItem() {
items.value?.add(UserItem())
items.postValue(items.value)
}
fun deleteItems() {
items.value?.removeAll { it.checked }
items.postValue(items.value)
}
fun checkItem(item: UserItem) {
item.checked = !item.checked
items.postValue(items.value)
}
fun deleteItem(item: UserItem) {
items.value?.remove(item)
items.postValue(items.value)
}
}
```
注意,这里通过实现`ItemClickListener`接口来处理勾选框和删除按钮的点击事件,并且使用了`MutableLiveData`来实现数据的双向绑定。
希望这个示例能对你有所帮助!