Just put these functions inside your RVadapter
public void onItemDismiss(int position) {
if(position!=-1 && position<mTask.size())
{
mTask.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount());
}
}
it provides the count of total elements currently present in the adapter
@Override
public int getItemCount() {
return (null != mTask ? mTask.size() : 0);
}
and in your onClick(), just add :
holder.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemDismiss(i)
}
});
Edit 1: Explanation
mTask.remove(position);
- removes the element at particular position from the List.
notifyItemRemoved(position);
- notifies the RecyclerView Adapter
that data in adapter has been removed at a particular position.
notifyItemRangeChanged(position, getItemCount());
- notifies the RecyclerView Adapter
that positions of element in adapter has been changed from position(removed element index to end of list), please update it.
if(position!=-1 && position<mTask.size())
- this condition verifies that the position of an element is not equal to -1 and the position of an element should be less than the size of total elements in the list. hence not causing an unwanted crash due to indexes of elements.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…