Android自定义SwipeLayout仿QQ侧滑条目

Android自定义SwipeLayout仿QQ侧滑条目,供大家参考,具体内容如下

先看动图

布局文件

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="www.weshared.qqcehua.MainActivity">

  <include layout="@layout/swipelayout" />

</RelativeLayout>

swipelayout.xml

<?xml version="1.0" encoding="utf-8"?>
<www.weshared.qqcehua.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/swipelayout"
  android:layout_width="match_parent"
  android:layout_height="72dp">

  <!--0  左边后布局-->
  <TextView
    android:id="@+id/back_left_tv_mark"
    android:layout_width="84dp"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_dark"
    android:gravity="center"
    android:text="Mark"
    android:textColor="@android:color/white"
    android:textSize="20sp" />

  <!--1  右边后布局-->
  <LinearLayout
    android:id="@+id/back_right_ll"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:orientation="horizontal">

    <TextView
      android:id="@+id/back_right_tv_call"
      android:layout_width="84dp"
      android:layout_height="match_parent"
      android:background="@android:color/holo_orange_dark"
      android:gravity="center"
      android:text="Call"
      android:textColor="@android:color/white"
      android:textSize="20sp" />

    <TextView
      android:id="@+id/back_right_tv_delete"
      android:layout_width="84dp"
      android:layout_height="match_parent"
      android:background="@android:color/holo_red_dark"
      android:gravity="center"
      android:text="Delete"
      android:textColor="@android:color/white"
      android:textSize="20sp" />

  </LinearLayout>

  <!--2  前布局 content-->
  <TextView
    android:id="@+id/front_tv_content"
    android:layout_width="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_height="match_parent"
    android:background="#666666" />
</www.weshared.qqcehua.SwipeLayout>

在MainActivity中

public class MainActivity extends AppCompatActivity {

  private SwipeLayout mSwipeLayout;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();

  }

  private void init() {
    mSwipeLayout = (SwipeLayout) findViewById(R.id.swipelayout);
    mSwipeLayout.setOnClickListener(new SwipeLayout.OnClickListener() {
      @Override
      public void onClick(View view) {
        switch (view.getId()) {
          case R.id.back_left_tv_mark:
            toast("mark");
            break;
          case R.id.front_tv_content:
            toast("content");
            break;
          case R.id.back_right_tv_call:
            toast("call");
            break;
          case R.id.back_right_tv_delete:
            toast("delete");
            break;
        }
      }
    });
  }

  public void toast(String message) {
    Toast toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
    if (!TextUtils.isEmpty(message) && toast != null) {
      toast.setText(message);
      toast.show();
    }
  }
}

自定义SwipeLayout控件

public class SwipeLayout extends FrameLayout {

  private View mBackLeftView;
  private ViewGroup mBackRightView;
  private View mFrontView;
  private int mWidth;
  private int mHeight;
  private int mLeftRange;
  private int mRightRange;
  private int status;
  public final int NORMAL = 0;//关闭状态
  public final int LEFT_OPEN = 1;//左边打开状态
  public final int RIGHT_OPEN = 2;//右边打开状态
  public final int LEFT_OPENING = 3;//左边正打开状态
  public final int LEFT_CLOSING = 4;//左边正关闭状态
  public final int RIGHT_OPENING = 5;//右边正打开状态
  public final int RIGHT_CLOSING = 6;//右边正关闭状态
  private ViewDragHelper mViewDrawHelper;
  private final int V = 300;//限制速度
  private OnSwipeListener mOnSwipeListener;
  private OnClickListener mOnClickListener;
  private View mBackRightCall;
  private View mBackRightDelete;
  private int x;
  private boolean isClick;
  private final int DX = 10;

  public interface OnSwipeListener {
    void onLeftOpen(SwipeLayout swipeLayout);

    void onLeftClose(SwipeLayout swipeLayout);

    void onRightOpen(SwipeLayout swipeLayout);

    void onRightClose(SwipeLayout swipeLayout);
  }

  public interface OnClickListener {
    void onClick(View view);
  }

  public SwipeLayout(Context context) {
    super(context);
    init();
  }

  public SwipeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();

  }

  public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
  }

  private void init() {
    status = NORMAL;//默认是NORMAL
    mViewDrawHelper = ViewDragHelper.create(this, callback);
  }

  public void setOnSwipeListener(OnSwipeListener mOnSwipeListener) {
    this.mOnSwipeListener = mOnSwipeListener;
  }

  public void setOnClickListener(OnClickListener mOnClickListener) {
    this.mOnClickListener = mOnClickListener;
  }

  private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
    /**是否试图拖拽子view*/
    @Override
    public boolean tryCaptureView(View child, int pointerId) {
      if (child == mBackLeftView) {
        return false;
      }
      return true;
    }

    /** 水平方向上的限制*/
    @Override
    public int clampViewPositionHorizontal(View child, int left, int dx) {

      if (child == mFrontView) {
        if (left < -mRightRange) {
          left = -mRightRange;
        } else if (left > mLeftRange) {
          left = mLeftRange;
        }
      } else if (child == mBackRightView) {
        if (left < mWidth - mRightRange) {
          left = mWidth - mRightRange;
        } else if (left > mWidth) {
          left = mWidth;
        }
      }
      return left;
    }

    /**这是系统定义的状态*/
    @Override
    public void onViewDragStateChanged(int state) {
      super.onViewDragStateChanged(state);

      if (state == ViewDragHelper.STATE_IDLE) {//ViewDrawHelper处于空闲状态

      } else if (state == ViewDragHelper.STATE_DRAGGING) {//ViewDrawHelper处于正在拖拽状态
        //拖拽状态,可设置滑动事件
      } else if (state == ViewDragHelper.STATE_SETTLING) {//ViewDrawHelper处于飞翔状态
        //飞翔状态设置,可设置滚动事件
      }
    }

    @Override
    public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
      super.onViewPositionChanged(changedView, left, top, dx, dy);

      if (mFrontView == changedView) {
        mBackRightView.offsetLeftAndRight(dx);
      } else if (mBackRightView == changedView) {
        mFrontView.offsetLeftAndRight(dx);
      }

      status = updateStatus();//更新控件的状态

      invalidate();//重绘界面
    }

    @Override
    public void onViewReleased(View releasedChild, float xvel, float yvel) {
      super.onViewReleased(releasedChild, xvel, yvel);

      int left = mFrontView.getLeft();
      if (left < -mRightRange * 0.5f) {
        if (xvel > V) {
          normalClose();
        } else {
          rightOpen();
        }
      } else if (left >= -mRightRange * 0.5f && left <= 0) {
        if (xvel < -V) {//向左滑动
          rightOpen();//打开右边前布局
        } else {
          normalClose();
        }
      } else if (left > 0 && left <= mLeftRange * 0.5f) {
        if (xvel > V) {
          leftOpen();
        } else {
          normalClose();
        }
      } else if (left > mLeftRange * 0.5f && left <= mLeftRange) {
        if (xvel < -V) {//向左滑动
          normalClose();
        } else {
          leftOpen();
        }
      }
    }
  };

  public void dispatchClickListener() {
    //设置点击事件
    if (status == LEFT_OPEN) {
      //mark的点击事件和Content点击事件
      if (mOnClickListener != null) {
        if (x > 0 && x < mLeftRange) {
          mOnClickListener.onClick(mBackLeftView);
        }
      }
    } else if (status == RIGHT_OPEN) {
      //call 和 Delete的点击事件 和Content点击事件
      if (mOnClickListener != null) {
        if (x > mWidth - mRightRange && x < mWidth - mRightRange * 0.5f) {
          mOnClickListener.onClick(mBackRightCall);
        } else if (x >= mWidth - mRightRange * 0.5f && x <= mWidth) {
          mOnClickListener.onClick(mBackRightDelete);
        }
      }
    } else if (status == NORMAL) {
      //content的点击事件
      if (mOnClickListener != null) {
        mOnClickListener.onClick(mFrontView);
      }
    }
  }

  private int updateStatus() {
    int left = mFrontView.getLeft();
    if (left == -mRightRange) {
      status = RIGHT_OPEN;
    } else if (left == 0) {
      status = NORMAL;
    } else if (left == mLeftRange) {
      status = LEFT_OPEN;
    }
    return status;
  }

  public void leftOpen() {
    leftOpen(true);
  }

  public void leftOpen(boolean isSmooth) {
    int finalLeft = mLeftRange;
    int finalTop = 0;
    if (isSmooth) {
      if (mViewDrawHelper.smoothSlideViewTo(mFrontView, finalLeft, finalTop)) {
        ViewCompat.postInvalidateOnAnimation(this);
      }
    } else {
      layoutContent(LEFT_OPEN);
    }
  }

  public void normalClose() {
    normalClose(true);
  }

  public void normalClose(boolean isSmooth) {
    int finalLeft = 0;
    int finalTop = 0;
    if (isSmooth) {
      if (mViewDrawHelper.smoothSlideViewTo(mFrontView, finalLeft, finalTop)) {
        ViewCompat.postInvalidateOnAnimation(this);
      }
    } else {
      layoutContent(NORMAL);
    }
  }

  public void rightOpen() {
    rightOpen(true);
  }

  public void rightOpen(boolean isSmooth) {
    int finalLeft = -mRightRange;
    int finalTop = 0;
    if (isSmooth) {
      if (mViewDrawHelper.smoothSlideViewTo(mFrontView, finalLeft, finalTop)) {
        ViewCompat.postInvalidateOnAnimation(this);
      }
    } else {
      layoutContent(RIGHT_OPEN);
    }
  }

  @Override
  public void computeScroll() {
    super.computeScroll();
    if (mViewDrawHelper.continueSettling(true)) {
      ViewCompat.postInvalidateOnAnimation(this);
    }
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    return mViewDrawHelper.shouldInterceptTouchEvent(ev);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {

    dispatchOnTouchEvent(event);//分发触摸的事件

    try {
      mViewDrawHelper.processTouchEvent(event);//将触摸事件传递给ViewDrawHelper
    } catch (Exception e) {
    }
    return true;
  }

  public void dispatchOnTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
      x = (int) event.getX();
      isClick = true;
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
      int movex = (int) event.getX();
      if (Math.abs(movex - x) > DX) {//防止点击事件,会稍微手指抖动
        isClick = false;
      }
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
      if (isClick) {
        dispatchClickListener();
      }
    }
  }

  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();

    //获取控件中的子控件
    mBackLeftView = getChildAt(0);
    mBackRightView = (ViewGroup) getChildAt(1);
    mFrontView = getChildAt(2);

    mBackRightCall = mBackRightView.getChildAt(0);
    mBackRightDelete = mBackRightView.getChildAt(1);
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    mWidth = getMeasuredWidth();
    mHeight = getMeasuredHeight();
    mLeftRange = mBackLeftView.getMeasuredWidth();
    mRightRange = mBackRightView.getMeasuredWidth();
  }

  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);

    //摆放布局
    layoutContent(NORMAL);
  }

  public void layoutContent(int status) {
    Rect frontRect = computeFrontRect(status);//根据状态,摆放前布局
    mFrontView.layout(frontRect.left, frontRect.top, frontRect.right, frontRect.bottom);

    Rect rightBackRect = computeRightBackRect(frontRect);//根据前布局,摆放右边后布局
    mBackRightView.layout(rightBackRect.left, rightBackRect.top, rightBackRect.right, rightBackRect.bottom);
  }

  public Rect computeFrontRect(int status) {
    int left = 0;
    int top = 0;
    if (status == LEFT_OPEN) {
      left = mLeftRange;
    } else if (status == RIGHT_OPEN) {
      left = -mRightRange;
    }
    return new Rect(left, top, left + mWidth, top + mHeight);
  }

  public Rect computeRightBackRect(Rect frontRect) {
    int left = 0;
    int top = 0;
    if (frontRect != null) {
      left = frontRect.right;
    }
    return new Rect(left, top, left + mRightRange, top + mHeight);
  }
}

现在控件耦合程度太高,以后慢慢优化,写成一个库。

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

(0)

相关推荐

  • Android仿京东分类模块左侧分类条目效果

    本文实例为大家分享了Android仿京东左侧分类条目效果的具体代码,供大家参考,具体内容如下 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; import com.frame.R;

  • android RecyclerView实现条目Item拖拽排序与滑动删除

    效果演示 需求和技术分析 RecyclerView Item拖拽排序::长按RecyclerView的Item或者触摸Item的某个按钮. RecyclerView Item滑动删除:RecyclerView Item滑动删除:RecyclerView的Item滑动删除. 实现方案与技术 利用ItemTouchHelper绑定RecyclerView.ItemTouchHelper.Callback来实现UI更新,并且实现动态控制是否开启拖拽功能和滑动删除功能. 实现步骤 继承抽象类ItemTo

  • Android条目拖拽删除功能实例代码

    项目中需求,要做条目条目拖拽删除效果,实际效果和QQ消息删除一样,侧滑有制定和删除. 效果图 第一步效果图 1.0自定义控件 SwipeLayout 继承FrameLayout重写里面三个构造方法,分别调用initView(). 2.0在布局中使用自定义控件 3.0在initView()方法中,创建拖拽辅辅助工具 ViewDragHelper() 该方法需要传入回调 MyCallBack() 4.0,创建MyCallBack()回调,继承ViewDragHelper.Callback 在回调中

  • Android实现下拉展示条目效果

    本文实例为大家分享了Android下拉展示条目的具体代码,供大家参考,具体内容如下 布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"

  • 详解Android中实现ListView左右滑动删除条目的方法

    使用Scroller实现绚丽的ListView左右滑动删除Item效果 这里来给大家带来使用Scroller的小例子,同时也能用来帮助初步解除的读者更加熟悉的掌握Scroller的使用,掌握好了Scroller的使用我们就能实现很多滑动的效果.例如侧滑菜单,launcher,ListView的下拉刷新等等效果,我今天实现的是ListView的item的左右滑动删除item的效果,现在很多朋友看到这个效果应该是在Android的通知栏下拉中看到这个滑动删除的效果吧,我看到这个效果是在我之前的三星手

  • Android编程实现canvas绘制饼状统计图功能示例【自动适应条目数量与大小】

    本文实例讲述了Android编程实现canvas绘制饼状统计图功能.分享给大家供大家参考,具体如下: 本例的目的是实现一个简单的饼状统计图,效果如下:    特点: 1.使用非常方便,可放在xml布局文件中,然后在代码中设置内容,即: PieChartView pieChartView = (PieChartView) findViewById(R.id.pie_chart); PieChartView.PieItemBean[] items = new PieChartView.PieItem

  • Android RecyclerView实现点击条目删除

    本文实例为大家分享了RecyclerView实现点击条目删除的具体代码,供大家参考,具体内容如下 MainActivity.java public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button mButton1; private Button mButton2; private Button mButton3; private Button mButton4

  • Android中listview和imageview实现条目单选效果

    前段时间在项目开发中,有listview实现单选和多选的效果,特别是listview的单选效果,一开始项目比较紧,自己考虑的是用listview和radionbutton实现的,可能是自己考虑不周到的原因,效果是实现了,但是用户体验不怎么好,做完项目后,自己又弄了下,使用listview和imageview实现,点击listview条目的时候就可以实现单选效果,这样用户体验就稍微好些.以下就是实现的方式: activity_main.xml文件: <RelativeLayout xmlns:an

  • Android 中 SwipeLayout一个展示条目底层菜单的侧滑控件源码解析

    由于项目上的需要侧滑条目展示收藏按钮,记得之前代码家有写过一个厉害的开源控件 AndroidSwipeLayout 本来准备直接拿来使用,但是看过 issue 发现现在有不少使用者反应有不少的 bug ,而且代码家现在貌似也不进行维护了.故自己实现了一个所要效果的一个控件.因为只是实现我需要的效果,所以大家也能看到,代码里有不少地方我是写死的.希望对大家有些帮助.而且暂时也不需要 AndroidSwipeLayout 大而全的功能,算是变相给自己做的项目精简代码了. 完整示例代码请看:GitHu

  • Android XRecyclerView实现多条目加载

    本文实例为大家分享了Android实现多条目加载展示的具体代码,供大家参考,具体内容如下 展示效果 这里写图片描述 依赖 testCompile 'junit:junit:4.12' compile 'com.hjm:BottomTabBar:1.1.1' compile 'com.android.support:design:23.4.0' compile 'com.android.support:mediarouter-v7:25.0.0' compile 'com.android.supp

  • Android更多条目收缩展开控件ExpandView的示例代码

    在Android开发中,我们经常使用列表控件,而有时候列表控件条目中又会是多条目数据,这时候,我们无法确定每个条目的数据多少,而为了美观,我们就希望条目统一高度,多数据的条目能够进行折叠.展开.今天,就为大家介绍一个这样的自定义控件 ExpandView. 效果演示图 演示图 Android Studio集成方式 dependencies{ compile 'com.wkp:ExpandView:1.0.4' //Android Studio3.0+可用以下方式 //implementation

  • Android中RecyclerView上拉下拉,分割线,多条目的实例代码

    //activity的xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity

  • Android ListView 条目多样式展示实例详解

    ListView的多种样式条目展示 这里给大家介绍一下简单的ListView的多种样式展示 在布局文件中和往常一样写一个ListViwe的布局 <ListView android:id="@+id/main_listview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 其他的这里就不多说了,直接介绍适配器里的操作 packag

  • Android ListView自动生成列表条目的实例

    activity_list.xml文件代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent

随机推荐