這篇文章給大家介紹利用android怎么實(shí)現(xiàn)一個左滑刪除控件,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)新互聯(lián)建站專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、丹寨網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為丹寨等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
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: //沒有指定大小 按照第一個子控件的大小來設(shè)置 height = mainChild.getMeasuredHeight(); break; case MeasureSpec.EXACTLY: height = heightValue; break; } switch (widthMode){ case MeasureSpec.AT_MOST: case MeasureSpec.UNSPECIFIED: //沒有指定大小 按照第一個子控件的大小來設(shè)置 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); //邊界判定 超過了邊界 直接設(shè)置為邊界值 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; //滑動距離超過 可滑動距離指定值 繼續(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); } //執(zhí)行滑動動畫必須先清除動畫 不然會鬼畜 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(); } } }
使用
關(guān)于利用android怎么實(shí)現(xiàn)一個左滑刪除控件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。