我有recycleview,我在其中實(shí)現(xiàn)了搜索功能。并且每個(gè)項(xiàng)目都添加了 onClick,它通過以下代碼從列表中刪除項(xiàng)目。 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());});它適用于普通列表,但當(dāng)搜索任何項(xiàng)目時(shí),在過濾結(jié)果上,onclick 偵聽器可能會(huì)由于視圖重疊而隨機(jī)刪除。所以我用下面的代碼將 onclick 偵聽器傳遞給活動(dòng) 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); }在我正在使用的活動(dòng)中 public void onDelete(Districtpost contact) { int newPosition = holder.getAdapterPosition(); contactListFiltered.remove(newPosition); notifyItemRemoved(newPosition); notifyItemRangeChanged(newPosition, contactListFiltered.size()); });這肯定會(huì)引發(fā)錯(cuò)誤。我該如何解決這個(gè)問題。提前致謝。
2 回答

哈士奇WWW
TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊
嘗試這個(gè)
public void onDelete(Districtpost contact) {
if(contactListFiltered != null && contactListFiltered.size > 0){
contactListFiltered.remove(contact);
notifydatasetchanged();
}});

慕的地8271018
TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
嘗試這個(gè):
yourList.remove(position); //Remove item from list
notifyItemRemoved(position); //notify changes made to the adapter.
添加回答
舉報(bào)
0/150
提交
取消