Android item长按删除功能
时间:2020-10-17 23:12:46|栏目:Android代码|点击: 次
这个可以在Adapter里面写
一般写完之后都会调用
public void removeItem(int pos){
this.mDatas.remove(pos);
notifyItemRemoved(pos);
}
可是这样写的话,删除多次,会乱套,没有刷新
所以改这样子写
public void removeItem(int pos){
this.mDatas.remove(pos);
notifyItemRemoved(pos);
if(pos != mDatas.size()){ // 如果移除的是最后一个,忽略
notifyItemRangeChanged(pos, mDatas.size() - pos);
}
}
PS:下面看下android 长按删除listview的item
首先要继承OnItemLongClickListener
public class Set_Music extends ListActivity implements OnItemLongClickListener{
然后设置权限:
getListView().setOnItemLongClickListener(this);
重写方法
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String str = list.get(arg2).get("name");
删掉长按的item
list.remove(arg2);
动态更新listview
adapter.notifyDataSetChanged();






