Android程序开发之ListView 与PopupWindow实现从左向右滑动删除功能

Android程序开发之ListView 与PopupWindow实现从左向右滑动删除功能,第1张

概述文章实现的功能是:在ListView的Item上从右向左滑时,出现删除按钮,点击删除按钮把Item删除。

文章实现的功能是:在ListVIEw的Item上从右向左滑时,出现删除按钮,点击删除按钮把Item删除。@H_419_1@

看过文章后,感觉没有必要把dispatchtouchEvent()和ontouchEvent()两个方法都重写,只要重写ontouchEvent就好了。于是对代码作了一些调整:@H_419_1@

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();}@OverrIDepublic 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() {@OverrIDepublic 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);}}

通过这个例子学习到:@H_419_1@

1、ListVIEw的Item点击事件的触发过程:@H_419_1@

自定义ListVIEw的ontouchEvent() ---调用super.ontouchEvent()---> AbsListVIEw.ontouchEvent() ---MotionEvent.ACTION_UP---> AbsListVIEw.ontouchUp()@H_419_1@

---(有可能)调用performClick.run()---> AbsListVIEw.PerformClick.run() ---调用performItemClick()---> AbsListVIEw.performItemClick()

---(有可能)调用super.performItemClick()---> AdapterVIEw.performItemClick() ---mOnItemClickListener.onItemClick---> OnItemClickListener.onItemClick()@H_419_1@

也就是Item的点击事件是在MotionEvent.ACTION_UP事件完成的,这样在自定义ListVIEw的ontouchEvent()中,对MotionEvent.ACTION_UP直接return true消费掉事件,而不要调用super.ontouchEvent。这样就避免了删除按钮与Item点击事件的冲突。@H_419_1@

2、PopupWindow--通过点击PopupWindow之外的地方使其消失@H_419_1@

a、需要调用setFocusable()方法;@H_419_1@

b、AndroID 6.0以前的版本需要setBackgroundDrawable()(具体原因见:PopupWindow的使用)。@H_419_1@

以上所述是小编给大家介绍的AndroID程序开发之ListVIEw 与PopupWindow实现滑动删除功能,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android程序开发之ListView 与PopupWindow实现从左向右滑动删除功能全部内容,希望文章能够帮你解决Android程序开发之ListView 与PopupWindow实现从左向右滑动删除功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1148844.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存