android实现简单左滑删除控件

这篇文章主要为大家详细介绍了android实现一个简单左滑删除控件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文为大家分享了一个简单的android左滑删除控件,供大家参考,具体内容如下

 import android.animation.ValueAnimator; import android.content.Context; import android.graphics.PointF; import android.support.v4.view.ViewConfigurationCompat; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; public class SwipeLayout extends ViewGroup{ public static String TAG = "SwipeLayout"; //可以滚动的距离 int mSwipeWidth; PointF firstPoint; PointF lastPoint; float mTouchSlop; ValueAnimator openAnimator; ValueAnimator closeAnimator; public SwipeLayout(Context context) { this(context,null); } public SwipeLayout(Context context, AttributeSet attrs) { super(context, attrs); mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(ViewConfiguration.get(getContext())); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int left=0; int childCount = getChildCount(); for (int i=0;i1) { for (int i = 1; i  mTouchSlop ){ //让父控件不拦截我们的事件 getParent().requestDisallowInterceptTouchEvent(true); //拦截事件 return true; } } return super.onInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()){ case MotionEvent.ACTION_MOVE: float moveDistance = ev.getX()-lastPoint.x; lastPoint = new PointF(ev.getX(),ev.getY()); // 这里要注意 x大于0的时候 往左滑动 小于0往右滑动 scrollBy((int) -moveDistance ,0); //边界判定 超过了边界 直接设置为边界值 if (getScrollX()> mSwipeWidth){ scrollTo(mSwipeWidth,0); }else if (getScrollX()<0){ scrollTo(0,0); } break; case MotionEvent.ACTION_UP: //没动 不理他 if (getScrollX()== mSwipeWidth ||getScrollX()==0){ return false; } float distance = ev.getX()-firstPoint.x; //滑动距离超过 可滑动距离指定值 继续完成滑动 if (Math.abs(distance) > mSwipeWidth *0.3 ){ if (distance>0){ smoothClose(); }else if (distance<0){ smoothOpen(); } }else { if (distance>0){ smoothOpen(); }else if (distance<0){ smoothClose(); } } return true; } return super.onTouchEvent(ev); } public void smoothOpen(){ clearAnimator(); openAnimator = ValueAnimator.ofInt(getScrollX(), mSwipeWidth); openAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { Integer integer = (Integer) animation.getAnimatedValue(); scrollTo(integer,0); } }); openAnimator.start(); } public void smoothClose(){ clearAnimator(); closeAnimator = ValueAnimator.ofInt(getScrollX(),0); closeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { Integer integer = (Integer) animation.getAnimatedValue(); scrollTo(integer,0); } }); closeAnimator.start(); } public void open(){ scrollTo(mSwipeWidth,0); } public void close(){ scrollTo(0,0); } //执行滑动动画必须先清除动画 不然会鬼畜 private void clearAnimator(){ if (closeAnimator!=null && closeAnimator.isRunning()){ closeAnimator.cancel(); closeAnimator = null; } if (openAnimator!=null && openAnimator.isRunning()) { openAnimator.cancel(); openAnimator = null; } } public void toggle(){ if (getScrollX()==0){ open(); }else { close(); } } }

使用

 

效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是android实现简单左滑删除控件的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动