pulltorefresh中listview的item怎么实现长按删除

2025-05-10 05:08:46
推荐回答(1个)
回答1:

删除事件


/** 
    * 删除Item 
    */  
   private void deleteItem() {  
       int size = mListItems.size();  
       if (size > 0) {  
           mListItems.remove(mDelId);  
           mAdapter.notifyDataSetChanged();  
       }  
   }


(1)mDelId是用于记录当前Item位置,以便删除相应的Item,该变量在前面已经定义


private static int mDelId = 0;

(2)remove函数系统已经定义,源码如下


public E remove(int location) {  
        if (location >= 0 && location < size) {  
            Link link = voidLink;  
            if (location < (size / 2)) {  
                for (int i = 0; i <= location; i++) {  
                    link = link.next;  
                }  
            } else {  
                for (int i = size; i > location; i--) {  
                    link = link.previous;  
                }  
            }  
            Link previous = link.previous;  
            Link next = link.next;  
            previous.next = next;  
            next.previous = previous;  
            size--;  
            modCount++;  
            return link.data;  
        }  
        throw new IndexOutOfBoundsException();  
    }


(3)注意使用notifyDataSetChanged方法用于Item的动态更新,源码如下。


public void notifyDataSetChanged() {  
        super.notifyDataSetChanged();  
        mNotifyOnChange = true;  
    }