【问题标题】:How to make delete button to delete recyclerview items in kotlin?如何制作删除按钮以删除kotlin中的recyclerview项目?
【发布时间】:2019-12-31 11:03:23
【问题描述】:

我在主要活动上创建了selectDelete 按钮,而不是RecyclerView。 当我单击该按钮时,我想删除选中的项目。 检查RecyclerView项目复选框并在滚动时取消选中项目时出现错误。


活动

class CartViewActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener {

    private val tag = this::class.java.simpleName

    lateinit var adapter: CartItemRecyclerAdapter

    var itemList: MutableList<CartItemDataVo> = arrayListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cart_view)

        selectDelete.setOnClickListener {

            if (checkBox.isChecked) {
                  *adapter.removeItems()*
            }
        }

        swipeRefreshLo.setOnRefreshListener(this)

        itemList.add(CartItemDataVo("item1", 1, 16800, "cart_doll", false))
        itemList.add(CartItemDataVo("item2", 1, 16800, "cart_cup", false))
        itemList.add(CartItemDataVo("item3", 1, 30000, "cart_perfume", false))
        itemList.add(CartItemDataVo("item4", 1, 16800, "cart_fan", false))

        adapter = CartItemRecyclerAdapter(this, this, itemList)
        recycler_view.adapter = adapter
        recycler_view.layoutManager =
            androidx.recyclerview.widget.LinearLayoutManager(applicationContext)
    }

    override fun onRefresh() {
        swipeRefreshLo.isRefreshing = false
    }
}

适配器:

class CartItemDataVo(
    title: String,
    itemNumber: Int,
    pointValue: Int,
    imageView: String,
    CheckBox: Boolean
) {
    var title: String = title
    var itemNumber: Int = itemNumber
    var pointValue: Int = pointValue
    var image: String = imageView
    var isChecked: Boolean = CheckBox
}

class CartItemRecyclerAdapter(
    val context: Context,
    private var activity: Activity,
    private var dataList: MutableList<CartItemDataVo>
) : RecyclerView.Adapter<CartItemRecyclerAdapter.Holder>() {

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

    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder?.bind(dataList[position], context)
    }

    override fun getItemCount(): Int = dataList.size


    *@SuppressLint("NewApi")
    fun removeItems() {
        dataList.removeIf { it.isChecked }
        notifyDataSetChanged()
     }*

    fun toggleItems() {
        for (item: CartItemDataVo in dataList) {
            var state = item.isChecked
            item.isChecked = state.not()
        }
        notifyDataSetChanged()
    }

    inner class Holder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {

        var titleText = itemView?.findViewById(R.id.titleText) as TextView
        var temNumerTextt = itemView?.findViewById(R.id.textViewItemNumer) as TextView
        var pointValueText = itemView?.findViewById(R.id.pointValueText) as TextView
        var imageView = itemView?.findViewById(R.id.imageView) as ImageView
        var checkBox = itemView?.findViewById(R.id.checkBox) as CheckBox

        fun bind(data: CartItemDataVo, context: Context) {
            if (data.image != "") {
                val resourceId =
                    context.resources.getIdentifier(data.image, "drawable", context.packageName)
                imageView?.setImageResource(resourceId)
            } else {
                imageView.setImageResource(R.mipmap.ic_launcher)
            }
            titleText?.text = data.title
            temNumerTextt?.text = data.itemNumber.toString()
            pointValueText?.text = data.pointValue.toString() + "P"

            if (data.isChecked) {
                checkBox.buttonDrawable =
                    checkBox.context.getDrawable(R.drawable.check_box_active_cs)
                val layout = activity?.findViewById(R.id.layoutOrder) as LinearLayout
                layout.visibility = View.VISIBLE
            } else {
                checkBox.buttonDrawable = checkBox.context.getDrawable(R.drawable.check_box_no)
                val layout = activity?.findViewById(R.id.layoutOrder) as LinearLayout
                layout.visibility = View.GONE
            }


            checkBox?.setOnClickListener {
                if (checkBox.isChecked == data.isChecked) {
                    checkBox.buttonDrawable = it.context.getDrawable(R.drawable.check_box_active_cs)
                    val layout = activity?.findViewById(R.id.layoutOrder) as LinearLayout
                    layout.visibility = View.VISIBLE
                } else {
                    checkBox.buttonDrawable = it.context.getDrawable(R.drawable.check_box_no)
                    val layout = activity?.findViewById(R.id.layoutOrder) as LinearLayout
                    layout.visibility = View.GONE
                }

            }
        }
    }
}

【问题讨论】:

  • 您需要设置复选框监听器,然后在您的模型上设置 data.isChecked。
  • 感谢您的回答。但我不明白你是什么意思。那么你能解释更详细的使用示例吗?
  • 你能指定R.id.layoutOrder是什么吗?您的代码总是隐藏或显示相同的视图(R.id.layoutOrder) 我认为您应该更改 itemView 的可见性
  • R.id.layoutOrder 对这个问题并不重要。它只是在选中项目复选框时显示。

标签: android kotlin checkbox android-recyclerview


【解决方案1】:

首先,替换

var itemList: MutableList&lt;CartItemDataVo&gt; = arrayListOf()

val itemList: MutableList&lt;CartItemDataVo&gt; = arrayListOf()

你不想要可变属性,它同时也是可变集合。这是非常糟糕的做法。

适配器中的相同情况

private var dataList : MutableList&lt;CartItemDataVo&gt;

替换为

private val dataList : MutableList&lt;CartItemDataVo&gt;


然后从适配器的构造函数中删除private var activity : Activity。不要在适配器中放置任何对ActivityFragment 的引用!

适配器应该只负责显示列表。

如果在任何情况下,您需要ActivityFragment 与适配器之间的通信,请改用接口。这不是ViewHolder 负责持有对父布局的引用并对其进行操作!

val layout = activity?.findViewById(R.id.layoutOrder) as LinearLayout

应从ViewHolder 中删除如下所有行。如果你需要设置属于Activity的东西,基于列表上的动作,使用接口。


最后,在适配器的 add 方法中删除选中的项目:

fun removeItems() {
    itemList.removeIf { it.isChecked }
    notifyDataSetChanged()
}

checkBox?.setOnClickListener { 之后添加以下行(作为侦听器的第一行)

data.isChecked = !data.isChecked

并替换

selectDelete.setOnClickListener {

    if(checkBox.isChecked){


    }
}

selectDelete.setOnClickListener {

    if(checkBox.isChecked){
        adapter?.removeItems()
    }
}

奖金:

  • 阅读 data class,并将其用于 CartItemDataVo(不要在构造函数中使用 CheckBox),

  • updateData 方法可以使用DiffUtil 优化RecyclerView

  • 可以通过修改Activity中的数据来改进,而不是在适配器中,所以负责人会被转移到更好的地方,

  • 了解 SOLID 原则,

  • 了解 MVVM 和 MVP 设计模式。

【讨论】:

  • 感谢您的详细解决方案,我只是将此解决方案应用于我的项目。但是当我单击 selectDelete 时,它​​可以删除所有项目。我想删除该复选框选中的项目。先生,我该怎么办?然后当我使用 removeif 时,为什么它需要 API 更新??
  • @DaesikPark 我已经更新了解决方案,使它更容易一些,但现在删除元素是适配器的责任(将此信息添加到奖金中)
  • 在 removeItems 函数中,itemList 替换为 dataList 对吗?,但它不起作用..
  • @DaesikPark 你自己解决了问题还是需要帮助?
  • 哦,我需要帮助。谢谢你。
猜你喜欢
  • 2020-07-29
  • 2021-04-11
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多