[英]How to remove item from Recycleview from activity
I have recycleview where I implemented the search features. 我在实现搜索功能的地方使用了recycleview。 and each item has onClick added which removes items from list by below code.
并且每个项目都添加了onClick,可通过以下代码从列表中删除项目。
holder.del_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int newPosition = holder.getAdapterPosition(); contactListFiltered.remove(newPosition); notifyItemRemoved(newPosition); notifyItemRangeChanged(newPosition, contactListFiltered.size()); });
It works fine with normal list but when any item is searched then on filtered results onclick listener deletes randomly may due to overlap of views. 它适用于普通列表,但是当搜索任何项目时,在筛选结果上,onclick侦听器可能会由于视图重叠而随机删除。 So I pass the onclick listener to activity with below code
所以我通过下面的代码将onclick侦听器传递给活动
mSolved.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // send selected contact in callback listener.onSolved(contactListFiltered.get(getAdapterPosition())); } }); public interface FRoomAdapterListener { void onSolved(Districtpost contact); }
and in activity I am using 在活动中我正在使用
public void onDelete(Districtpost contact) { int newPosition = holder.getAdapterPosition(); contactListFiltered.remove(newPosition); notifyItemRemoved(newPosition); notifyItemRangeChanged(newPosition, contactListFiltered.size()); });
which is certain to throw errors. 这肯定会引发错误。 How can I solve this.
我该如何解决。 Thanks in advance.
提前致谢。
try this 尝试这个
public void onDelete(Districtpost contact) {
if(contactListFiltered != null && contactListFiltered.size > 0){
contactListFiltered.remove(contact);
notifydatasetchanged();
}});
Try this: 尝试这个:
yourList.remove(position); //Remove item from list
notifyItemRemoved(position); //notify changes made to the adapter.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.