ListView + PopupWindow实现滑动删除
文章实现的功能是:在ListView的Item上从右向左滑时,出现删除按钮,点击删除按钮把Item删除。
看过文章后,感觉没有必要把dispatchTouchEvent()和onTouchEvent()两个方法都重写,只要重写onTouchEvent就好了。于是对代码作了一些调整:
public class MyListView extends ListView {
private static final String TAG = "MyListView";
private int mTouchSlop;
private int mXDown;
private int mYDown;
private int mCurrentPosition;
private View mCurrentView;
private PopupWindow mPopupWindow;
private LayoutInflater mInflater;
private boolean isSliding = false;
// 为删除按钮提供一个回调接口
private DelButtonClickListener mListener;
private Button mDelBtn;
private int mPopupWindowHeight;
private int mPopupWindowWidth;
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
mInflater = LayoutInflater.from(context);
mTouchSlop = 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);
// 如果需要通过点击PopupWindow之外的地方使其消失,则需要setFocusable(true).
mPopupWindow.setFocusable(true);
// Android 6.0以前的版本需要setBackgroundDrawable(),
// 才能实现通过点击PopupWindow之外的地方使其消失的功能。
mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
// 先调用下measure,否则拿不到宽和高
mPopupWindow.getContentView().measure(0, 0);
mPopupWindowHeight = mPopupWindow.getContentView().getMeasuredHeight();
mPopupWindowWidth = mPopupWindow.getContentView().getMeasuredWidth();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = ev.getAction();
int x = (int) ev.getX();
int y = (int) ev.getY();
switch (action){
case MotionEvent.ACTION_DOWN:
isSliding = false;
mXDown = x;
mYDown = y;
mCurrentPosition = pointToPosition(mXDown, mYDown);
View view = getChildAt(mCurrentPosition - getFirstVisiblePosition());
mCurrentView = view;
break;
case MotionEvent.ACTION_MOVE:
int dx = x - mXDown;
int dy = y - mYDown;
Log.d(TAG, "mTouchSlop = " + mTouchSlop + ", dx = " + dx + ", dy = " + dy);
if(mXDown > x && Math.abs(dx) > mTouchSlop && Math.abs(dy) < mTouchSlop){
Log.d(TAG, "isSliding");
isSliding = true;
int[] location = new int[2];
mCurrentView.getLocationOnScreen(location);
mPopupWindow.setAnimationStyle(R.style.popwindow_delete_btn_anim_style);
mPopupWindow.update();
Log.d(TAG, "Height: " + mCurrentView.getHeight() + "," + mPopupWindow.getHeight());
mPopupWindow.showAtLocation(mCurrentView, Gravity.NO_GRAVITY,
location[0] + mCurrentView.getWidth(),
location[1] + mCurrentView.getHeight() / 2 - mPopupWindowHeight / 2);
mDelBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.clickHappend(mCurrentPosition);
mPopupWindow.dismiss();
}
});
}
case MotionEvent.ACTION_UP:
// isSliding 如果这里恢复为false,则后面会执行super.onTouchEvent事件,
// 而AbsListView的onTouchEvent调用了onTouchUp方法,在onTouchUp方法中有可能执行
// performClick.run() --> performItemClick() --> super.performItemClick
// --> mOnItemClickListener.onItemClick,这样最终触发Item的点击。
// 因此此处依旧保持isSliding为true的状态,而在ACTION_DOWN事件中恢复isSliding为false,
// 毕竟每个事件都以ACTION_DOWN开始。
//isSliding = false;
}
if(isSliding){
return true;
}
return super.onTouchEvent(ev);
}
public void setDelButtonClickListener(DelButtonClickListener listener){
mListener = listener;
}
interface DelButtonClickListener{
public void clickHappend(int position);
}
}
MyListView.java
通过这个例子学习到:
1、ListView的Item点击事件的触发过程:
自定义ListView的onTouchEvent() ---调用super.onTouchEvent()---> AbsListView.onTouchEvent() ---MotionEvent.ACTION_UP---> AbsListView.onTouchUp()
---(有可能)调用performClick.run()---> AbsListView.PerformClick.run() ---调用performItemClick()---> AbsListView.performItemClick()
---(有可能)调用super.performItemClick()---> AdapterView.performItemClick() ---mOnItemClickListener.onItemClick---> OnItemClickListener.onItemClick()
也就是Item的点击事件是在MotionEvent.ACTION_UP事件完成的,这样在自定义ListView的onTouchEvent()中,对MotionEvent.ACTION_UP直接return true消费掉事件,而不要调用super.onTouchEvent。这样就避免了删除按钮与Item点击事件的冲突。
2、PopupWindow--通过点击PopupWindow之外的地方使其消失
a、需要调用setFocusable()方法(PopupWindow中showAtLocation() --> createPopupLayoutParams() --> computeFlags() --> 设置FLAG_NOT_FOCUSABLE);
b、Android 6.0以前的版本需要setBackgroundDrawable()(具体原因见:PopupWindow的使用)。
ListView + PopupWindow实现滑动删除的更多相关文章
- Android 用HorizontalScrollView实现ListView的Item滑动删除 ,滑动错乱 冲突
用HorizontalScrollView实现类似微信的滑动删除 测试于:Android2.2+ 对于Android来说按键操作已经在减少,越来越多的手势操作层出不穷,今天介绍一款LIstView的I ...
- Android 高级UI设计笔记03:使用ListView实现左右滑动删除Item
1. 这里就是实现一个很简单的功能,使用ListView实现左右滑动删除Item: (1)当我们在ListView的某个Item,向左滑动显示一个删除按钮,用户点击按钮,即可以删除该项item,并且有 ...
- android中listview的item滑动删除效果(已解决listview点击问题)
领导看到iphone上tableview有个滑动删除的效果,要求在android上也实现,搜了下资料,实现起来比较简单,可弄到后面,居然不能点击了,把一篇文章中的代码修改了一下,捣鼓了一番,搞定,下面 ...
- Android 使用NineOldAndroids实现绚丽的ListView左右滑动删除Item效果
本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/18311877) 今天还是给大家带来自定义控件的编写,自定义一个Lis ...
- 自定义listView添加滑动删除功能
今天研究了一下android里面的手势,结合昨天学习的自定义View,做了一个自定义的listview,继承自listView,添加了条目的滑动手势操作,滑动后出现一个删除按钮,点击删除按钮,触发一个 ...
- android滑动删除的多种实现方式(一)
个人习惯,先上图 同事是个妹子(这点很重要),写滑动删除动能的时候用到了SwipeLayout,然后悲催的是,滑动时间被拦截了,解决方法先不提,在(一)中先讲解SwipeLayout下载listvie ...
- ListView滑动删除效果实现
通过继承ListView然后结合PopupWindow实现 首先是布局文件: delete_btn.xml:这里只需要一个Button <?xml version="1.0" ...
- 【转】Android 实现ListView的滑动删除效果
http://www.cnblogs.com/weixiao870428/p/3524055.html http://download.csdn.net/download/love_javc_you/ ...
- 【转】Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果
原文网址:http://blog.csdn.net/xiaanming/article/details/17539199 转帖请注明本文出自xiaanming的博客(http://blog.csdn. ...
随机推荐
- iOS开发——UI基础-控制器,IBAction和IBOutlet,UIView
第一个ios程序 @interface ViewController : UIViewController @property(nonatomic, weak)IBOutlet UILabel *la ...
- C语言 homework (3)
#include<stdio.h> #include<stdlib.h> #include<time.h> int main() { ; char c; do{ ...
- eclipse中整合springMvc,velocity和sitemesh
1.项目所需要jar包 (有些可能多余) 2.创建UserController 目录如下: package qust.thb.usermanage.controller; import org.s ...
- Android活动管理工具
ActivityCollector.java import android.app.Activity; import java.util.ArrayList; import java.util.Lis ...
- Android 中的 Intent 简介
Intent是Android程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据. ------------------------------- ...
- ubuntu 16.04 挂起后WiFi链接不上
在笔记本上安装ubuntu 16.04后,使用挂起系统功能后发现WIFI链接不上去,然后使用以下指令多WIFI服务进行重启,发觉可以了. sudo service network-manager re ...
- PageImpl是不是有问题?
pageable.getOffset() + content.size() : total这个API 感觉没有实现该有的功能!!!
- Lua简易入门教程
环境:lua for windows (lfW)主页:http://luaforwindows.luaforge.net/https://code.google.com/p/luaforwindows ...
- Binary Tree Upside Down
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- rsa加密解密
2016年3月17日 17:21:08 星期四 现在越来越懒了.... 参考: http://www.xuebuyuan.com/1399981.html 左边是加密流程, 右边是解密流程 呃...有 ...