在Android中實(shí)現(xiàn)多選項(xiàng)視圖的動(dòng)態(tài)增刪

小樊
83
2024-09-05 20:38:31

在 Android 中,要實(shí)現(xiàn)一個(gè)可以動(dòng)態(tài)增刪多選項(xiàng)的視圖,可以使用 RecyclerView 和相關(guān)的 Adapter。以下是一個(gè)簡(jiǎn)單的示例:

  1. 首先,創(chuàng)建一個(gè)表示選項(xiàng)的數(shù)據(jù)類:
data class Option(val id: Int, val text: String, var isSelected: Boolean = false)
  1. 創(chuàng)建一個(gè)用于顯示選項(xiàng)的布局(例如:option_item.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"
    android:padding="16dp">

   <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

   <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Option Text" />

</LinearLayout>
  1. 創(chuàng)建一個(gè)用于管理選項(xiàng)的 Adapter:
class OptionsAdapter(private val options: MutableList<Option>) :
    RecyclerView.Adapter<OptionsAdapter.ViewHolder>() {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val checkbox: CheckBox = itemView.findViewById(R.id.checkbox)
        val textView: TextView = itemView.findViewById(R.id.textView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.option_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val option = options[position]
        holder.checkbox.isChecked = option.isSelected
        holder.textView.text = option.text
        holder.itemView.setOnClickListener {
            option.isSelected = !option.isSelected
            holder.checkbox.isChecked = option.isSelected
        }
    }

    override fun getItemCount(): Int = options.size
}
  1. 在 Activity 或 Fragment 中設(shè)置 RecyclerView:
val options = mutableListOf<Option>()
// 添加初始選項(xiàng)
options.add(Option(1, "Option 1"))
options.add(Option(2, "Option 2"))
options.add(Option(3, "Option 3"))

val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
val adapter = OptionsAdapter(options)
recyclerView.adapter = adapter
  1. 添加按鈕以動(dòng)態(tài)增刪選項(xiàng):
val addButton: Button = findViewById(R.id.addButton)
val removeButton: Button = findViewById(R.id.removeButton)

addButton.setOnClickListener {
    val newOption = Option(options.size + 1, "New Option ${options.size + 1}")
    options.add(newOption)
    adapter.notifyItemInserted(options.size - 1)
}

removeButton.setOnClickListener {
    if (options.isNotEmpty()) {
        options.removeAt(options.size - 1)
        adapter.notifyItemRemoved(options.size)
    }
}

這樣,你就可以通過(guò)點(diǎn)擊 “添加” 和 “刪除” 按鈕來(lái)動(dòng)態(tài)地向 RecyclerView 中添加或刪除多選項(xiàng)了。

0