我已通过更改选定项目的背景颜色来实现回收者视图中的多个项目选择。当我从模型中删除这些项目时,项目会被删除,但突出显示不会随着项目的删除而消失,而是会被应用在其余项目上。
我尝试从模型中删除项目,并在每次删除时通知适配器,如下所示
for (item in selectedItems) {
deleteResidentItem(item.key, sharedPreferences.getString("AuthToken", SHARED_PREFERENCE_DOES_NOT_EXIST))
removeSelectedItemsFromModelById(item.key)
residentRecyclerViewAdapter.notifyItemRemoved(item.value)
residentRecyclerViewAdapter.notifyItemRangeChanged(item.value, mutableResidentList.size)
}
即使删除了该项目,突出显示的项目仍会随机出现在列表中,如下例所示
我希望从模型中删除突出显示的视图后,如何解决此问题?
适配器实现代码
class ResidentRecyclerViewAdapter( val residentItems: List<ResidentListModel>):RecyclerView.Adapter<ResidentRecyclerViewAdapter.ResidentViewHolder>() {
lateinit var residentRecyclerViewListenerInterface:ResidentRecyclerViewListenerInterface
fun initResidentRecyclerViewListenerInterface(listener:ResidentRecyclerViewListenerInterface) {
residentRecyclerViewListenerInterface = listener
}
override fun onCreateViewHolder(parent: ViewGroup, p1: Int): ResidentViewHolder {
var layoutInflater = LayoutInflater.from(parent.context)
var inflatedLayout = layoutInflater.inflate(R.layout.single_resident_item, parent, false)
return ResidentViewHolder(inflatedLayout, residentRecyclerViewListenerInterface)
}
override fun getItemCount() = residentItems.size
override fun onBindViewHolder(holder: ResidentViewHolder, posistion:Int) {
//place to bind the values
val ModelName = residentItems.get(posistion).ModelName
val VechicleNo = residentItems.get(posistion).VehicleNo
val SlNo = residentItems.get(posistion).SlNo
holder.vehicle_number.text = VechicleNo
holder.model_name.text = ModelName
holder.sl_no.text = SlNo.toString()
holder.resident_item_wrapper.setOnLongClickListener {
when (holder.is_selected.isChecked) {
// not checked, then check, add to list
false-> {
holder.resident_item_wrapper.setBackgroundColor(Color.parseColor("#1565c0"))
holder.resident_vehicle_number.setTextColor(Color.WHITE)
holder.resident_model_name.setTextColor(Color.WHITE)
holder.is_selected.setBackgroundColor(Color.WHITE)
holder.is_selected.visibility = View.VISIBLE
holder.is_selected.isChecked = true
residentRecyclerViewListenerInterface.onResidentItemLongClickListener(holder.sl_no.text.toString().toInt(), posistion)
}
true -> {
// checked then unselect and remove from list
holder.resident_item_wrapper.setBackgroundColor(Color.WHITE)
holder.resident_vehicle_number.setTextColor(Color.BLACK)
holder.resident_model_name.setTextColor(Color.BLACK)
holder.is_selected.setBackgroundColor(Color.BLACK)
holder.is_selected.visibility = View.GONE
holder.is_selected.isChecked = false
residentRecyclerViewListenerInterface.onResidentItemLongUnselectClickListener(holder.sl_no.text.toString().toInt(),posistion)
}
}
true
}
}
interface ResidentRecyclerViewListenerInterface {
fun onResidentItemLongClickListener(Id:Int, Position:Int)
fun onResidentItemClickListener(Id:Int, Position: Int)
fun onResidentItemLongUnselectClickListener(Id: Int, Position: Int)
}
class ResidentViewHolder(val view: View, residentRecyclerViewListenerInterface:ResidentRecyclerViewListenerInterface):RecyclerView.ViewHolder(view) {
var vehicle_number:TextView
var model_name:TextView
var sl_no:TextView
var resident_item_wrapper:CardView
var is_selected:CheckBox
var resident_vehicle_number:TextView
var resident_model_name:TextView
lateinit var residentRecyclerViewListenerInterface:ResidentRecyclerViewListenerInterface
init {
vehicle_number = view.resident_vehicle_number
model_name = view.resident_model_name
sl_no = view.resident_slno
resident_item_wrapper = view.resident_item_wrapper
is_selected = view.is_selected
resident_vehicle_number = view.resident_vehicle_number
resident_model_name = view.resident_model_name
}
}
}
答案 0 :(得分:2)
尝试一下:
在您的POJO类中添加一个Boolean
变量
public class POJO {
boolean isSelected;
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
在您的onBindViewHolder()
方法中进行以下更改
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if(pojoArrayList.get(position).isSelected()){
// make selection in your item
}else {
//remove selction from you item
}
}
现在在您的onLongClickListener
中,使您的选择正确
示例代码
sampleButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
// set the status of selection
pojoArrayList.get(position).setSelected(true);
return true;
}
});
当您要删除选择内容时,请使用此
pojoArrayList.get(position).setSelected(false);
,当您想从列表中删除项目时,请使用该boolean
变量删除项目
if(pojoArrayList.get(position).isSelected()){
//remove the item from list
// and notifyDataSetChanged(); after removing the item from list
}
答案 1 :(得分:2)
您的问题出在when (holder.is_selected.isChecked) {
如果在ViewModel上而不是在View上(最肯定是不在ViewHolder中)检查了项目,则应该具有此信息。
应该是if(residentItems.get(posistion).isSelected){
之类的东西(在二进制情况下,使用when
是过分的了)