在前面两篇文章已经介绍了如何使用PullToRefresh方法以及Item的点击事件,
文章1:PullToRefresh的使用方法http://blog.csdn.net/u012721519/article/details/51108406
文章2:ItemClicker的点击事件http://blog.csdn.net/u012721519/article/details/51112560
本文主要介绍长按点击删除功能的实现。
1:长按弹框事件
/** * 长按Item事件 */ mPullRefreshListView.setOnItemLongClickListener(new OnItemLongClickListener() { public boolean onItemLongClick(AdapterView<?> arg0, View view, final int location, long arg3) { Toast.makeText(MainActivity.this, mListItems.get(location - 1).toString(), Toast.LENGTH_LONG).show(); mDelId = location - 1; showDialog(); return false; } });
以上具体讲解请看前面文章http://blog.csdn.net/u012721519/article/details/51112560
2、删除事件
/** * 删除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<E> 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<E> previous = link.previous; Link<E> 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; }
3、Demo效果图如下
图一 删除Jimmy3 Item 图二 删除后效果
Good luck!
Write by Jimmy.li