溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用android怎么實現(xiàn)一個左滑刪除控件

發(fā)布時間:2021-01-19 16:41:00 來源:億速云 閱讀:348 作者:Leah 欄目:移動開發(fā)

這篇文章給大家介紹利用android怎么實現(xiàn)一個左滑刪除控件,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

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;i<childCount;++i){
   View child = getChildAt(i);
 
   //按順序從左往右排
//   if (i==0){
//    child.layout(0,0,child.getMeasuredWidth(),child.getMeasuredHeight());
//   }else {
    child.layout(left,0,left+child.getMeasuredWidth(),child.getMeasuredHeight());
//   }
   left += child.getMeasuredWidth();
  }
 
 
 }
 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int childCount = getChildCount();
  View mainChild = getChildAt(0);
  int width=0;
  int height=0;
  mSwipeWidth = 0;
//  measureChild(mainChild,widthMeasureSpec,heightMeasureSpec);
  measure(widthMeasureSpec,heightMeasureSpec);
 
  //滑動距離是 從index開始 所有控件的寬度之和
  if (childCount>1) {
   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<childCount;++i){
//   measureChild(getChildAt(i),widthMeasureSpec,MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY));
//  }
  setMeasuredDimension(width,height);
 }
 
 
 @Override
 public boolean dispatchTouchEvent(MotionEvent ev) {
  return super.dispatchTouchEvent(ev);
 }
 
 @Override
 public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()){
   case MotionEvent.ACTION_DOWN:
    firstPoint = new PointF(ev.getX(),ev.getY());
    lastPoint = new PointF(ev.getX(),ev.getY());
    break;
   case MotionEvent.ACTION_MOVE:
    float moveDistance = ev.getX()-firstPoint.x;
 
    //移動距離大于制定值 認為進入控件的滑動模式
    if (Math.abs(moveDistance) > 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();
  }
 }
 
}

使用

<com.example.chenweiqi.simplerefreshview.widget.SwipeLayout
  android:id="@+id/swipeLayout"
  android:layout_width="200dp"
  android:layout_height="wrap_content"
  android:background="#F3F3F3"
>
<Button
  android:id="@+id/btn"
  android:text="123"
  android:layout_width="match_parent"
  android:layout_height="50dp" />
 
<Button
  android:background="#FF0000"
  android:text="shanchu"
  android:layout_width="80dp"
  android:layout_height="match_parent" />
<TextView
  android:gravity="center"
  android:textAlignment="center"
  android:background="#0F0"
  android:text="123"
  android:layout_width="30dp"
  android:layout_height="match_parent" />
</com.example.chenweiqi.simplerefreshview.widget.SwipeLayout>

關(guān)于利用android怎么實現(xiàn)一個左滑刪除控件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI