android ListView横向滑动删除Item

来源:互联网 发布:linux 运维之道第2版 编辑:程序博客网 时间:2024/06/05 16:50

注册ListView的onTouchListener()

具体代码如下


listview.setOnTouchListener(new OnTouchListener() {
float x,y,ux,uy;
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_DOWN){
x=event.getX();
y=event.getY();
}
if(event.getAction()==MotionEvent.ACTION_UP){
ux=event.getX();uy=event.getY();
int p1=store_history.pointToPosition((int)x, (int)y);
int p2=store_history.pointToPosition((int)ux, (int)uy);
if(p1==p2&&Math.abs(x-ux)>10){

                                                   //p1为item的position,得到position了,删除应该没问题了吧

    
//Cursor cursor = (Cursor) houseAdapter.getItem(p1);
//Contacts.mLBSService.deleteFavorite(DBUtil.cursorToHouseInfo(cursor).getId(), null);
//Log.d("sad", "cursor requery : " + cursor.requery());

                                                  listviewadapter.getData().remove(p1);
listviewadapter.notifyDataSetChanged();
}
return true;
}
return false;
}
});




下面为完整demo

 零积分下载页面

原创粉丝点击