今天学习了新的功能那就是滑动删除数据。先看一下效果
我想这个效果大家都很熟悉吧。是不是在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滑动删除数据功能的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
# android
# 滑动删除
# Android开发中模仿qq列表信息滑动删除功能
# Android 滑动监听RecyclerView线性流+左右划删除+上下移动
# Android仿腾讯QQ实现滑动删除 附源码下载
# Android RecyclerView滑动删除和拖动排序
# Android实现ListView左右滑动删除和编辑
# Android App中ListView仿QQ实现滑动删除效果的要点解析
# 按下
# 是从
# 表单
# 回调
# 小编
# 我想
# 大家都
# 在此
# 见过
# 我把
# 跟我
# 给大家
# 上看
# 提供一个
# 所述
# 先看
# 给我留言
# 俗话说
# 很熟悉
# 感谢大家
相关文章:
建站之星如何实现PC+手机+微信网站五合一建站?
南京网站制作费用,南京远驱官方网站?
网站制作软件免费下载安装,有哪些免费下载的软件网站?
建站之星如何助力企业快速打造五合一网站?
建站之星后台密码如何安全设置与找回?
网站制作新手教程,新手建设一个网站需要注意些什么?
如何用手机制作网站和网页,手机移动端的网站能制作成中英双语的吗?
西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?
建站为何优先选择香港服务器?
SQL查询语句优化的实用方法总结
建站VPS推荐:2025年高性能服务器配置指南
如何在七牛云存储上搭建网站并设置自定义域名?
网站制作哪家好,cc、.co、.cm哪个域名更适合做网站?
攀枝花网站建设,攀枝花营业执照网上怎么年审?
公众号网站制作网页,微信公众号怎么制作?
制作网站的过程怎么写,用凡科建站如何制作自己的网站?
网站制作公司排行榜,抖音怎样做个人官方网站
如何选择香港主机高效搭建外贸独立站?
如何在Windows 2008云服务器安全搭建网站?
北京网站制作公司哪家好一点,北京租房网站有哪些?
已有域名如何快速搭建专属网站?
建站主机与虚拟主机有何区别?如何选择最优方案?
如何通过可视化优化提升建站效果?
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
北京的网站制作公司有哪些,哪个视频网站最好?
单页制作网站有哪些,朋友给我发了一个单页网站,我应该怎么修改才能把他变成自己的呢,请求高手指点迷津?
三星网站视频制作教程下载,三星w23网页如何全屏?
,想在网上投简历,哪几个网站比较好?
教学论文网站制作软件有哪些,写论文用什么软件
?
潍坊网站制作公司有哪些,潍坊哪家招聘网站好?
如何在阿里云ECS服务器部署织梦CMS网站?
专业商城网站制作公司有哪些,pi商城官网是哪个?
建站之星价格显示格式升级,你的预算足够吗?
郑州企业网站制作公司,郑州招聘网站有哪些?
如何在西部数码注册域名并快速搭建网站?
微信小程序制作网站有哪些,微信小程序需要做网站吗?
制作旅游网站html,怎样注册旅游网站?
电商平台网站制作流程,电商网站如何制作?
重庆网站制作公司哪家好,重庆中考招生办官方网站?
javascript基本数据类型及类型检测常用方法小结
建站之星在线版空间:自助建站+智能模板一键生成方案
5种Android数据存储方式汇总
如何快速搭建虚拟主机网站?新手必看指南
建站之星2.7模板快速切换与批量管理功能操作指南
,sp开头的版面叫什么?
北京网站制作的公司有哪些,北京白云观官方网站?
建站之星如何助力网站排名飙升?揭秘高效技巧
官网自助建站系统:SEO优化+多语言支持,快速搭建专业网站
建站之星伪静态规则如何设置?
子杰智能建站系统|零代码开发与AI生成SEO优化指南
*请认真填写需求信息,我们会在24小时内与您取得联系。