今天学习了新的功能那就是滑动删除数据。先看一下效果
我想这个效果大家都很熟悉吧。是不是在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" 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","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滑动删除数据功能的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android滑动删除数据功能的实现代码全部内容,希望文章能够帮你解决Android滑动删除数据功能的实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)