Android滑动删除数据功能的实现代码
程序员文章站
2024-02-20 22:50:47
今天学习了新的功能那就是滑动删除数据。先看一下效果
我想这个效果大家都很熟悉吧。是不是在qq上看见过这个效果。俗话说好记性不如赖笔头,为了我的以后,为了跟我一样自...
今天学习了新的功能那就是滑动删除数据。先看一下效果
我想这个效果大家都很熟悉吧。是不是在qq上看见过这个效果。俗话说好记性不如赖笔头,为了我的以后,为了跟我一样自学的小伙伴们,我把我的代码粘贴在下面。
activity_lookstaff.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/tv_title" style="@style/gtextview" android:text="全部员工" /> <com.rjxy.view.deletelistview android:id="@+id/id_listview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_title"> </com.rjxy.view.deletelistview> </relativelayout>
delete_btn.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <button android:id="@+id/id_item_btn" android:layout_width="60dp" android:singleline="true" android:layout_height="wrap_content" android:text="删除" android:background="@drawable/d_delete_btn" android:textcolor="#ffffff" android:paddingleft="15dp" android:paddingright="15dp" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="15dp" /> </linearlayout>
d_delete_btn.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_style_five_focused" android:state_focused="true"></item> <item android:drawable="@drawable/btn_style_five_pressed" android:state_pressed="true"></item> <item android:drawable="@drawable/btn_style_five_normal"></item> </selector>
deletelistview .java
package com.rjxy.view; import com.rjxy.activity.r; import android.content.context; import android.util.attributeset; import android.view.gravity; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.view.viewconfiguration; import android.widget.button; import android.widget.linearlayout; import android.widget.listview; import android.widget.popupwindow; public class deletelistview extends listview { private static final string tag = "deletelistview"; /** * 用户滑动的最小距离 */ private int touchslop; /** * 是否响应滑动 */ private boolean issliding; /** * 手指按下时的x坐标 */ private int xdown; /** * 手指按下时的y坐标 */ private int ydown; /** * 手指移动时的x坐标 */ private int xmove; /** * 手指移动时的y坐标 */ private int ymove; private layoutinflater minflater; private popupwindow mpopupwindow; private int mpopupwindowheight; private int mpopupwindowwidth; private button mdelbtn; /** * 为删除按钮提供一个回调接口 */ private delbuttonclicklistener mlistener; /** * 当前手指触摸的view */ private view mcurrentview; /** * 当前手指触摸的位置 */ private int mcurrentviewpos; /** * 必要的一些初始化 * * @param context * @param attrs */ public deletelistview(context context, attributeset attrs) { super(context, attrs); minflater = layoutinflater.from(context); touchslop = viewconfiguration.get(context).getscaledtouchslop(); view view = minflater.inflate(r.layout.delete_btn, null); mdelbtn = (button) view.findviewbyid(r.id.id_item_btn); mpopupwindow = new popupwindow(view, linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content); /** * 先调用下measure,否则拿不到宽和高 */ mpopupwindow.getcontentview().measure(0, 0); mpopupwindowheight = mpopupwindow.getcontentview().getmeasuredheight(); mpopupwindowwidth = mpopupwindow.getcontentview().getmeasuredwidth(); } @override public boolean dispatchtouchevent(motionevent ev) { int action = ev.getaction(); int x = (int) ev.getx(); int y = (int) ev.gety(); switch (action) { case motionevent.action_down: xdown = x; ydown = y; /** * 如果当前popupwindow显示,则直接隐藏,然后屏蔽listview的touch事件的下传 */ if (mpopupwindow.isshowing()) { dismisspopwindow(); return false; } // 获得当前手指按下时的item的位置 mcurrentviewpos = pointtoposition(xdown, ydown); // 获得当前手指按下时的item view view = getchildat(mcurrentviewpos - getfirstvisibleposition()); mcurrentview = view; break; case motionevent.action_move: xmove = x; ymove = y; int dx = xmove - xdown; int dy = ymove - ydown; /** * 判断是否是从右到左的滑动 */ if (xmove < xdown && math.abs(dx) > touchslop && math.abs(dy) < touchslop) { // log.e(tag, "touchslop = " + touchslop + " , dx = " + dx + // " , dy = " + dy); issliding = true; } break; } return super.dispatchtouchevent(ev); } @override public boolean ontouchevent(motionevent ev) { int action = ev.getaction(); /** * 如果是从右到左的滑动才相应 */ if (issliding) { switch (action) { case motionevent.action_move: int[] location = new int[2]; // 获得当前item的位置x与y mcurrentview.getlocationonscreen(location); // 设置popupwindow的动画 mpopupwindow.setanimationstyle(r.style.popwindow_delete_btn_anim_style); mpopupwindow.update(); mpopupwindow.showatlocation(mcurrentview, gravity.left | gravity.top, location[0] + mcurrentview.getwidth(), location[1] + mcurrentview.getheight() / 2 - mpopupwindowheight / 2); // 设置删除按钮的回调 mdelbtn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (mlistener != null) { mlistener.clickhappend(mcurrentviewpos); mpopupwindow.dismiss(); } } }); // log.e(tag, "mpopupwindow.getheight()=" + mpopupwindowheight); break; case motionevent.action_up: issliding = false; } // 相应滑动期间屏幕itemclick事件,避免发生冲突 return true; } return super.ontouchevent(ev); } /** * 隐藏popupwindow */ private void dismisspopwindow() { if (mpopupwindow != null && mpopupwindow.isshowing()) { mpopupwindow.dismiss(); } } public void setdelbuttonclicklistener(delbuttonclicklistener listener) { mlistener = listener; } public interface delbuttonclicklistener { public void clickhappend(int position); } }
deletestaffactivity .java
package com.rjxy.activity; import java.io.ioexception; import java.io.inputstream; import java.net.httpurlconnection; import java.net.url; import java.util.arraylist; import java.util.list; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import com.rjxy.bean.staff; import com.rjxy.path.path; import com.rjxy.util.streamtools; import com.rjxy.view.deletelistview; import com.rjxy.view.deletelistview.delbuttonclicklistener; import android.app.activity; import android.os.bundle; import android.os.handler; import android.os.message; import android.view.view; import android.view.window; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.toast; public class deletestaffactivity extends activity { private static final int change_ui = 1; private static final int delete = 3; private static final int success = 2; private static final int error = 0; private deletelistview lv; private arrayadapter<string> madapter; private list<string> staffs = new arraylist<string>(); private staff staff; string sno; // 主线程创建消息处理器 private handler handler = new handler() { public void handlemessage(android.os.message msg) { if (msg.what == change_ui) { try { jsonarray arr = new jsonarray((string) msg.obj); for (int i = 0; i < arr.length(); i++) { jsonobject temp = (jsonobject) arr.get(i); staff = new staff(); staff.setsno(temp.getstring("sno")); staff.setsname(temp.getstring("sname")); staff.setdname(temp.getstring("d_name")); staffs.add("员工号:" + staff.getsno() + "\n姓 名:" + staff.getsname() + "\n部 门:" + staff.getdname()); } madapter = new arrayadapter<string>( deletestaffactivity.this, android.r.layout.simple_list_item_1, staffs); lv.setadapter(madapter); lv.setdelbuttonclicklistener(new delbuttonclicklistener() { @override public void clickhappend(final int position) { string s = madapter.getitem(position); string[] ss = s.split("\n"); string snos = ss[0]; string[] sss = snos.split(":"); sno = sss[1]; delete(); madapter.remove(madapter.getitem(position)); } }); lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { toast.maketext( deletestaffactivity.this, position + " : " + madapter.getitem(position), 0) .show(); } }); } catch (jsonexception e) { e.printstacktrace(); } } else if (msg.what == delete) { toast.maketext(deletestaffactivity.this, (string) msg.obj, 1) .show(); } }; }; @override protected void oncreate(bundle savedinstancestate) { requestwindowfeature(window.feature_no_title); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_lookstaff); lv = (deletelistview) findviewbyid(r.id.id_listview); select(); } private void select() { // 子线程更新ui new thread() { public void run() { try { // 区别1、url的路径不同 url url = new url(path.lookstaffpath); httpurlconnection conn = (httpurlconnection) url .openconnection(); // 区别2、请求方式post conn.setrequestmethod("post"); conn.setrequestproperty("user-agent", "mozilla/5.0(compatible;msie 9.0;windows nt 6.1;trident/5.0)"); // 区别3、必须指定两个请求的参数 conn.setrequestproperty("content-type", "application/x-www-form-urlencoded");// 请求的类型 表单数据 string data = ""; conn.setrequestproperty("content-length", data.length() + "");// 数据的长度 // 区别4、记得设置把数据写给服务器 conn.setdooutput(true);// 设置向服务器写数据 byte[] bytes = data.getbytes(); conn.getoutputstream().write(bytes);// 把数据以流的方式写给服务器 int code = conn.getresponsecode(); system.out.println(code); if (code == 200) { inputstream is = conn.getinputstream(); string result = streamtools.readstream(is); message mas = message.obtain(); mas.what = change_ui; mas.obj = result; handler.sendmessage(mas); } else { message mas = message.obtain(); mas.what = error; handler.sendmessage(mas); } } catch (ioexception e) { // todo auto-generated catch block message mas = message.obtain(); mas.what = error; handler.sendmessage(mas); } } }.start(); } private void delete() { // 子线程更新ui new thread() { public void run() { try { // 区别1、url的路径不同 url url = new url(path.deletestaffpath); httpurlconnection conn = (httpurlconnection) url .openconnection(); // 区别2、请求方式post conn.setrequestmethod("post"); conn.setrequestproperty("user-agent", "mozilla/5.0(compatible;msie 9.0;windows nt 6.1;trident/5.0)"); // 区别3、必须指定两个请求的参数 conn.setrequestproperty("content-type", "application/x-www-form-urlencoded");// 请求的类型 表单数据 string data = "sno=" + sno; conn.setrequestproperty("content-length", data.length() + "");// 数据的长度 // 区别4、记得设置把数据写给服务器 conn.setdooutput(true);// 设置向服务器写数据 byte[] bytes = data.getbytes(); conn.getoutputstream().write(bytes);// 把数据以流的方式写给服务器 int code = conn.getresponsecode(); system.out.println(code); if (code == 200) { inputstream is = conn.getinputstream(); string result = streamtools.readstream(is); message mas = message.obtain(); mas.what = delete; mas.obj = result; handler.sendmessage(mas); } else { message mas = message.obtain(); mas.what = error; handler.sendmessage(mas); } } catch (ioexception e) { // todo auto-generated catch block message mas = message.obtain(); mas.what = error; handler.sendmessage(mas); } } }.start(); } }
以上所述是小编给大家介绍的android滑动删除数据功能的实现代码,希望对大家有所帮助
上一篇: C#实现修改系统时间的方法
下一篇: MySql安装启动两种方法教程详解