Android仿微信对话列表滑动删除效果

Android仿微信对话列表滑动删除效果,第1张

概述微信对话列表滑动删除效果很不错的,借鉴了github上SwipeListView(项目地址:https://github.com/likebamboo/SwipeListView),在其上进行了一些重构,最终实现了微信对话列表滑动删除效果。

微信对话列表滑动删除效果很不错的,借鉴了github上SwipeListVIEw(项目地址:https://github.com/likebamboo/SwipeListView),在其上进行了一些重构,最终实现了微信对话列表滑动删除效果。

实现原理
 1.通过ListVIEw的pointToposition(int x,int y)来获取按下的position,然后通过androID.vIEw.VIEwGroup.getChildAt(position)来得到滑动对象swipeview
 2.在ontouchEvent中计算要滑动的距离,调用swipeview.scrollTo即可。

运行效果如下

下面是最核心的部分SwipeListVIEw代码: 

package com.fxsky.swipeList.Widget;import androID.annotation.Suppresslint;import androID.content.Context;import androID.content.res.TypedArray;import androID.os.Handler;import androID.os.Message;import androID.util.AttributeSet;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.Widget.ListVIEw;import com.fxsky.swipeList.R;public class SwipeListVIEw extends ListVIEw { private Boolean mIsHorizontal; private VIEw mPreItemVIEw; private VIEw mCurrentItemVIEw; private float mFirstX; private float mFirstY; private int mRightVIEwWIDth; // private boolean mIsInAnimation = false; private final int mDuration = 100; private final int mDurationStep = 10; private boolean mIsShown; public SwipeListVIEw(Context context) { this(context,null); } public SwipeListVIEw(Context context,AttributeSet attrs) { this(context,attrs,0); } public SwipeListVIEw(Context context,AttributeSet attrs,int defStyle) { super(context,defStyle);  TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.swipeListvIEwstyle);   //获取自定义属性和默认值  mRightVIEwWIDth = (int) mTypedArray.getDimension(R.styleable.swipeListvIEwstyle_right_wIDth,200);   mTypedArray.recycle();  } /** * return true,deliver to ListVIEw. return false,deliver to child. if * move,return true */ @OverrIDe public boolean onIntercepttouchEvent(MotionEvent ev) { float lastX = ev.getX(); float lastY = ev.getY(); switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mIsHorizontal = null; System.out.println("onIntercepttouchEvent----->ACTION_DOWN"); mFirstX = lastX; mFirstY = lastY; int motionposition = pointToposition((int)mFirstX,(int)mFirstY); if (motionposition >= 0) {  VIEw currentItemVIEw = getChildAt(motionposition - getFirstVisibleposition());  mPreItemVIEw = mCurrentItemVIEw;  mCurrentItemVIEw = currentItemVIEw; } break; case MotionEvent.ACTION_MOVE: float dx = lastX - mFirstX; float dy = lastY - mFirstY; if (Math.abs(dx) >= 5 && Math.abs(dy) >= 5) {  return true; } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: System.out.println("onIntercepttouchEvent----->ACTION_UP"); if (mIsShown && (mPreItemVIEw != mCurrentItemVIEw || isHitCurItemleft(lastX))) {  System.out.println("1---> hIDdenRight");  /**  * 情况一:  * <p>  * 一个Item的右边布局已经显示,  * <p>  * 这时候点击任意一个item,那么那个右边布局显示的item隐藏其右边布局  */  hIDdenRight(mPreItemVIEw); } break; } return super.onIntercepttouchEvent(ev); } private boolean isHitCurItemleft(float x) { return x < getWIDth() - mRightVIEwWIDth; } /** * @param dx * @param dy * @return judge if can judge scroll direction */ private boolean judgeScrollDirection(float dx,float dy) { boolean canJudge = true; if (Math.abs(dx) > 30 && Math.abs(dx) > 2 * Math.abs(dy)) { mIsHorizontal = true; System.out.println("mIsHorizontal---->" + mIsHorizontal); } else if (Math.abs(dy) > 30 && Math.abs(dy) > 2 * Math.abs(dx)) { mIsHorizontal = false; System.out.println("mIsHorizontal---->" + mIsHorizontal); } else { canJudge = false; } return canJudge; } /** * return false,can't move any direction. return true,cant't move * vertical,can move horizontal. return super.ontouchEvent(ev),can move * both. */ @OverrIDe public boolean ontouchEvent(MotionEvent ev) { float lastX = ev.getX(); float lastY = ev.getY(); switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: System.out.println("---->ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: float dx = lastX - mFirstX; float dy = lastY - mFirstY; // confirm is scroll direction if (mIsHorizontal == null) {  if (!judgeScrollDirection(dx,dy)) {  break;  } } if (mIsHorizontal) {  if (mIsShown && mPreItemVIEw != mCurrentItemVIEw) {  System.out.println("2---> hIDdenRight");  /**  * 情况二:  * <p>  * 一个Item的右边布局已经显示,  * <p>  * 这时候左右滑动另外一个item,那个右边布局显示的item隐藏其右边布局  * <p>  * 向左滑动只触发该情况,向右滑动还会触发情况五  */  hIDdenRight(mPreItemVIEw);  }  if (mIsShown && mPreItemVIEw == mCurrentItemVIEw) {  dx = dx - mRightVIEwWIDth;  System.out.println("======dx " + dx);  }  // can't move beyond boundary  if (dx < 0 && dx > -mRightVIEwWIDth) {  mCurrentItemVIEw.scrollTo((int)(-dx),0);  }  return true; } else {  if (mIsShown) {  System.out.println("3---> hIDdenRight");  /**  * 情况三:  * <p>  * 一个Item的右边布局已经显示,  * <p>  * 这时候上下滚动ListVIEw,那么那个右边布局显示的item隐藏其右边布局  */  hIDdenRight(mPreItemVIEw);  } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: System.out.println("============ACTION_UP"); clearpressedState(); if (mIsShown) {  System.out.println("4---> hIDdenRight");  /**  * 情况四:  * <p>  * 一个Item的右边布局已经显示,  * <p>  * 这时候左右滑动当前一个item,那个右边布局显示的item隐藏其右边布局  */  hIDdenRight(mPreItemVIEw); } if (mIsHorizontal != null && mIsHorizontal) {  if (mFirstX - lastX > mRightVIEwWIDth / 2) {  showRight(mCurrentItemVIEw);  } else {  System.out.println("5---> hIDdenRight");  /**  * 情况五:  * <p>  * 向右滑动一个item,且滑动的距离超过了右边VIEw的宽度的一半,隐藏之。  */  hIDdenRight(mCurrentItemVIEw);  }  return true; } break; } return super.ontouchEvent(ev); } private voID clearpressedState() { // Todo current item is still has background,issue mCurrentItemVIEw.setpressed(false); setpressed(false); refreshDrawableState(); // invalIDate(); } private voID showRight(VIEw vIEw) { System.out.println("=========showRight"); Message msg = new MoveHandler().obtainMessage(); msg.obj = vIEw; msg.arg1 = vIEw.getScrollX(); msg.arg2 = mRightVIEwWIDth; msg.sendToTarget(); mIsShown = true; } private voID hIDdenRight(VIEw vIEw) { System.out.println("=========hIDdenRight"); if (mCurrentItemVIEw == null) { return; } Message msg = new MoveHandler().obtainMessage();// msg.obj = vIEw; msg.arg1 = vIEw.getScrollX(); msg.arg2 = 0; msg.sendToTarget(); mIsShown = false; } /** * show or hIDe right layout animation */ @Suppresslint("HandlerLeak") class MoveHandler extends Handler { int stepX = 0; int fromX; int toX; VIEw vIEw; private boolean mIsInAnimation = false; private voID animatioOver() { mIsInAnimation = false; stepX = 0; } @OverrIDe public voID handleMessage(Message msg) { super.handleMessage(msg); if (stepX == 0) { if (mIsInAnimation) {  return; } mIsInAnimation = true; vIEw = (VIEw)msg.obj; fromX = msg.arg1; toX = msg.arg2; stepX = (int)((toX - fromX) * mDurationStep * 1.0 / mDuration); if (stepX < 0 && stepX > -1) {  stepX = -1; } else if (stepX > 0 && stepX < 1) {  stepX = 1; } if (Math.abs(toX - fromX) < 10) {  vIEw.scrollTo(toX,0);  animatioOver();  return; } } fromX += stepX; boolean isLastStep = (stepX > 0 && fromX > toX) || (stepX < 0 && fromX < toX); if (isLastStep) { fromX = toX; } vIEw.scrollTo(fromX,0); invalIDate(); if (!isLastStep) { this.sendEmptyMessageDelayed(0,mDurationStep); } else { animatioOver(); } } } public int getRightVIEwWIDth() { return mRightVIEwWIDth; } public voID setRightVIEwWIDth(int mRightVIEwWIDth) { this.mRightVIEwWIDth = mRightVIEwWIDth; }}

Demo下载地址:http://xiazai.jb51.net/201608/yuanma/SwipeListView(jb51.net).rar

Demo中SwipeAdapter源码中有一处由于粗心写错了,会导致向下滑动时出现数组越界异常,现更正如下:

@OverrIDe public int getCount() {// return 100; return data.size(); }

本文已被整理到了《Android微信开发教程汇总》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android仿微信对话列表滑动删除效果全部内容,希望文章能够帮你解决Android仿微信对话列表滑动删除效果所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存