利用android怎么实现一个左滑删除控件
这篇文章给大家介绍利用android怎么实现一个左滑删除控件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
创新互联建站专注于企业成都全网营销、网站重做改版、丹寨网站定制设计、自适应品牌网站建设、H5网站设计、商城网站建设、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为丹寨等各大城市提供网站开发制作服务。
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 < childCount; ++i) { mSwipeWidth += getChildAt(i).getMeasuredWidth(); } } int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthValue = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightValue = MeasureSpec.getSize(heightMeasureSpec); switch (heightMode){ case MeasureSpec.AT_MOST: case MeasureSpec.UNSPECIFIED: //没有指定大小 按照第一个子控件的大小来设置 height = mainChild.getMeasuredHeight(); break; case MeasureSpec.EXACTLY: height = heightValue; break; } switch (widthMode){ case MeasureSpec.AT_MOST: case MeasureSpec.UNSPECIFIED: //没有指定大小 按照第一个子控件的大小来设置 width = mainChild.getMeasuredWidth(); break; case MeasureSpec.EXACTLY: width = widthValue; break; } // 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(); } } }
使用
关于利用android怎么实现一个左滑删除控件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
网页标题:利用android怎么实现一个左滑删除控件
文章链接:http://lswzjz.com/article/pdhsid.html