我在RecyclerAdapter上遇到另一个问题,我花了几个小时思考如何解决它,但最终我还是在这里寻求帮助。 :)
我想要的是具有以下功能的自定义RecyclerAdapter:
我试图从我的主活动中调用“ notifyDataSetChanged”,然后从onBindViewHolder中调用“ 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
}
}
感谢所有帮助! :)