Android / Kotlin自定义RecyclerAdapter-ViewHolder视图更新

时间:2018-07-19 19:00:00

标签: android android-recyclerview kotlin recycler-adapter

我在RecyclerAdapter上遇到另一个问题,我花了几个小时思考如何解决它,但最终我还是在这里寻求帮助。 :)

我想要的是具有以下功能的自定义RecyclerAdapter:

  1. 如果我长按任何一项,则复选框将出现在每行旁边
  2. 每个复选框都可以彼此独立选择
  3. 连同复选框,工具栏上出现两个按钮,以确认或取消删除操作
  4. 确认删除后,我想使用适当的动画从适配器数据集中删除项目,所以我需要使用notifyItemRemoved函数

我试图从我的主活动中调用“ notifyDataSetChanged”,然后从onBindViewHolder中调用“ notifyItemRemoved”,但这给了我一个例外。

我的问题是:

  1. 每次我要更新RecyclerAdapter视图时是否需要调用notifyDataSetChanged?
  2. 如何在多个项目上调用notifyItemRemoved?

我的下面的代码:

class ProductsRecyclerAdapter(private val dataSet: ArrayList<ProductEntry>, private val context: Context) : RecyclerView.Adapter<ProductsRecyclerAdapter.ViewHolder>() {

// Some static values
companion object {
    var productsToDeleteArray: HashMap<Int, ProductEntry> = HashMap()
    var checkMode = false
}

// Interface used to communicate with Main Activity
interface ProductsRecyclerAdapterListener {

    fun onLongProductClick()
}

inner class ViewHolder(val linearLay: LinearLayout) : RecyclerView.ViewHolder(linearLay)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : ProductsRecyclerAdapter.ViewHolder {


    val linearLay = LayoutInflater.from(parent.context).inflate(R.layout.product_list_row, parent, false) as LinearLayout

    // Handle long click which may end with item deletion
    if(!linearLay.hasOnClickListeners()) { // Check if there is already a listener

        linearLay.setOnLongClickListener {

            // Change checkMode state on long press
            if(!checkMode) {

                checkMode = true

                // Refresh adapter
                notifyDataSetChanged()

                // If state of rows is changed via long click, check if buttons on toolbar should be visible or hidden
                val productsRecyclerAdapter = context as ProductsRecyclerAdapterListener

                productsRecyclerAdapter.onLongProductClick()
            }

            true
        }
    }

    return ViewHolder(linearLay)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    // Set TextViews to correct values
    holder.linearLay.productNameRow.text = dataSet[position].pName

    holder.linearLay.productExpiryRow.text = dataSet[position].pExpiry


    // Show/Hide checkboxes
    if (checkMode) {

        holder.linearLay.checkBox.visibility = View.VISIBLE

        holder.linearLay.checkBox.isChecked = false

        // Handle checking boxes by user
        if (!holder.linearLay.checkBox.hasOnClickListeners()) {

            holder.linearLay.checkBox.setOnCheckedChangeListener { _, isChecked ->

                if (isChecked) {

                    // Change layout, red background and white letters
                    holder.linearLay.setBackgroundColor(Color.RED)
                    holder.linearLay.productNameRow.setTextColor(Color.WHITE)
                    holder.linearLay.productExpiryRow.setTextColor(Color.BLACK)

                    // Add product to deletion list
                    productsToDeleteArray[position] = dataSet[position]

                } else {

                    // Change layout to default
                    holder.linearLay.setBackgroundColor(Color.WHITE)
                    holder.linearLay.productNameRow.setTextColor(ResourcesCompat.getColor(context.resources, R.color.fontColorSingleRow, null))
                    holder.linearLay.productExpiryRow.setTextColor(ResourcesCompat.getColor(context.resources, R.color.fontColorSingleRow, null))

                    // Delete product from deletion list
                    productsToDeleteArray.remove(position)
                }
            }
        }
    } else {

        // Hide checkboxes
        holder.linearLay.checkBox.visibility = View.GONE

        // Bring back default colors
        holder.linearLay.setBackgroundColor(Color.WHITE)
        holder.linearLay.productNameRow.setTextColor(ResourcesCompat.getColor(context.resources, R.color.fontColorSingleRow, null))
        holder.linearLay.productExpiryRow.setTextColor(ResourcesCompat.getColor(context.resources, R.color.fontColorSingleRow, null))

        // Clear array with items to delete
        productsToDeleteArray.clear()

        // Delete listener
        holder.linearLay.checkBox.setOnCheckedChangeListener(null)
    }

}

override fun getItemCount(): Int {

    // Return size of dataSet
    return dataSet.size
}

}

感谢所有帮助! :)

0 个答案:

没有答案