效果图
实现功能
1,可以向左滑动,右侧出现删除
2,向左滑动如果删除出现一大半,松手打开删除,反之关闭删除
3,应用场景
微信消息的删除功能
实现原理
1,外面是一个ListView
2,条目是一个自定义控件继承ViewGroup
1,左边一个TextView,右侧屏幕外也有一个TextView
2,所以继承ViewGroup
实现步骤
1,创建一个SlideDeleteView类
1,构造方法要关联
public class SlideDelete extends ViewGroup {
private View leftView;
private View rightView;
private ViewDragHelper helper;
public SlideDelete(Context context) {
this(context,null);
}
public SlideDelete(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public SlideDelete(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
helper = ViewDragHelper.create(this, callback);
}
}
2,在布局文件中设置SlideDeleteView里面的子View
SlideDeleteView height=80
TextView
width:matchParent
height:matchparent
TextView
<com.example.movedelete.SlideDelete
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="80dp">
"@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#33000000"
android:text="第0个条目"
android:textColor="#fff"
android:textSize="20sp" />
"@+id/delete"
android:layout_width="80dp"
android:layout_height="match_parent"
android:background="#f00"
android:gravity="center"
android:text="删除"
android:textColor="#fff"
android:textSize="20sp" />
com.example.movedelete.SlideDelete>
3,重写onMeasure,给子View进行测量
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureChildren(widthMeasureSpec,heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
4,重写onLayout,给子View进行布局
注意事项:
要设置leftView,也要设置rightView,不要都写成leftView了
protected void onLayout(boolean changed, int l, int t, int r, int b) {
leftView = getChildAt(0);
rightView = getChildAt(1);
int leftL = 0;
int leftT = 0;
int leftR = leftView.getMeasuredWidth();
int leftB = leftView.getMeasuredHeight();
leftView.layout(leftL,leftT,leftR,leftB);
int rightL = leftView.getMeasuredWidth();
int rightT = 0;
int rightR = leftView.getMeasuredWidth()+ rightView.getMeasuredWidth();
int rightB = rightView.getMeasuredHeight();
rightView.layout(rightL,rightT,rightR,rightB);
}
5,设置View的滑动事件onTouchEvent,实现滑动
1,因为要滑动,所以消费该事件返回true
2,使用ViewDragHelper来实现滑动效果
注意事项:
只能实现leftView的滑动,右侧RightView看不到所以滑动不了
只能给滑动的View设置监听,当滑动的时候,重新设置另一个View的布局跟着滑动
@Override
public boolean onTouchEvent(MotionEvent event) {
helper.processTouchEvent(event);
return true;
}
6,重写滑动事件的监听onViewPositionChanged解决只有lefView滑动的问题
1,重写的方法是在ViewDragHelper.Callback的子实现类中
2,要实现滑动事件,必须在tryCaptureView方法中返回true
private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return true;
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
if(changedView == leftView){
rightView.layout(rightView.getLeft()+dx,0,rightView.getRight()+dx,rightView.getBottom()+dy);
}else if(changedView == rightView){
leftView.layout(leftView.getLeft()+dx,0,leftView.getRight()+dx,leftView.getBottom()+dy);
}
}
7,重写clampViewPositionHorizontal水平位置移动,解决左右越界问题
1,返回值为移动时左侧滑动的距离
2,如果滑动的控件是leftView时,解决越界
3,如果滑动的控件是rightView时,解决越界
public int clampViewPositionHorizontal(View child, int left, int dx) {
if(child == leftView){
if(left >= 0){
left = 0;
}else if(left <= -rightView.getMeasuredWidth()){
left = -rightView.getMeasuredWidth();
}
}else if(child == rightView){
if(left <= leftView.getMeasuredWidth()- rightView.getMeasuredWidth()){
left = leftView.getMeasuredWidth()- rightView.getMeasuredWidth();
}else if(left >= leftView.getMeasuredWidth()){
left = leftView.getMeasuredWidth();
}
}
return left;
}
8,手松开时重写onViewReleased方法,实现滑动手松开时,rightView是打开还是关闭
1,使用ViewDragHelper滑动时,要调用invalidate方法,回调computeScroll方法
2,重写computeScroll方法
1,先判断是否要继承滑动
2,使用兼容的invalidate方法来实现匀速滑动
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
if(releasedChild == leftView){
if(rightView.getLeft() < getMeasuredWidth() - rightView.getMeasuredWidth()/2){
helper.smoothSlideViewTo(rightView,getMeasuredWidth()-rightView.getMeasuredWidth(),0);
invalidate();
}else{
helper.smoothSlideViewTo(rightView,getMeasuredWidth(),0);
invalidate();
}
}
}
@Override
public void computeScroll() {
if(helper.continueSettling(true)){
ViewCompat.postInvalidateOnAnimation(this);
}
}
9,实现删除rightView的点击删除事件
1,在listView的adapter中找到右侧的rightView
2,调用rightView的点击事件
3,删除该条目
1,删除集合中的数据
list.remove(position);
2,更新adapter
notifyDataSetChanged();
3,重新绘制整个条目
requestLayout();
vh.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
notifyDataSetChanged();
}
});
vh.container.requestLayout();
注意事项
1,在重写onLayout方法的时候,给rightView设置布局的时候,写成leftView一直出错
2,在onTouchEvent中要返回true,因为要消费该事件
3,在使用ViewDragHelper.Callback时,重写tryCaptureView时要返回true
4,在onTouchEvent中,不使用scrollby或者scrollTo,而是使用ViewDragHelper工具类,不需要判断滑动的距离
总结
1,首先该控件是自定义View,不是组合控件,因为组合的话rightView在屏幕右侧不能实现
2,是自定义View中的继承ViewGroup,因为左侧leftView和右侧rightView都是TextView不需要自己画
源码
SlideDelete的源码
package com.example.movedelete;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by guixin on 2017/1/5.
*/
public class SlideDelete extends ViewGroup {
private View leftView;
private View rightView;
private ViewDragHelper helper;
public SlideDelete(Context context) {
this(context,null);
}
public SlideDelete(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public SlideDelete(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
helper = ViewDragHelper.create(this, callback);
}
private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return true;
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if(child == leftView){
if(left >= 0){
left = 0;
}else if(left <= -rightView.getMeasuredWidth()){
left = -rightView.getMeasuredWidth();
}
}else if(child == rightView){
if(left <= leftView.getMeasuredWidth()- rightView.getMeasuredWidth()){
left = leftView.getMeasuredWidth()- rightView.getMeasuredWidth();
}else if(left >= leftView.getMeasuredWidth()){
left = leftView.getMeasuredWidth();
}
}
return left;
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
if(changedView == leftView){
rightView.layout(rightView.getLeft()+dx,0,rightView.getRight()+dx,rightView.getBottom()+dy);
}else if(changedView == rightView){
leftView.layout(leftView.getLeft()+dx,0,leftView.getRight()+dx,leftView.getBottom()+dy);
}
}
/**
*
* @param releasedChild 松开的View
* @param xvel
* @param yvel
*/
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
if(releasedChild == leftView){
if(rightView.getLeft() < getMeasuredWidth() - rightView.getMeasuredWidth()/2){
helper.smoothSlideViewTo(rightView,getMeasuredWidth()-rightView.getMeasuredWidth(),0);
invalidate();
}else{
helper.smoothSlideViewTo(rightView,getMeasuredWidth(),0);
invalidate();
}
}
}
};
@Override
public void computeScroll() {
if(helper.continueSettling(true)){
ViewCompat.postInvalidateOnAnimation(this);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureChildren(widthMeasureSpec,heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
leftView = getChildAt(0);
rightView = getChildAt(1);
int leftL = 0;
int leftT = 0;
int leftR = leftView.getMeasuredWidth();
int leftB = leftView.getMeasuredHeight();
leftView.layout(leftL,leftT,leftR,leftB);
int rightL = leftView.getMeasuredWidth();
int rightT = 0;
int rightR = leftView.getMeasuredWidth()+ rightView.getMeasuredWidth();
int rightB = rightView.getMeasuredHeight();
rightView.layout(rightL,rightT,rightR,rightB);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
helper.processTouchEvent(event);
return true;
}
}
MainActivity.java源码
package com.example.movedelete;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import com.example.movedelete.adapter.SlideDeleteAdapter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView lv;
private ArrayList list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initEvent();
}
private void initView() {
lv = (ListView) findViewById(R.id.lv);
}
private void initData() {
list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add("第"+i+"项条目");
}
}
private void initEvent() {
SlideDeleteAdapter adapter = new SlideDeleteAdapter(list);
lv.setAdapter(adapter);
}
}
SlideDeleteAdapter.java源码
package com.example.movedelete.adapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.movedelete.R;
import com.example.movedelete.SlideDelete;
import java.util.ArrayList;
/**
* Created by guixin on 2017/1/5.
*/
public class SlideDeleteAdapter extends BaseAdapter{
private ArrayList list;
public SlideDeleteAdapter(ArrayList list) {
this.list = list;
}
@Override
public int getCount() {
return list == null ? 0 : list.size();
}
@Override
public String getItem(int position) {
return list == null ? null : list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if(convertView == null){
convertView = View.inflate(parent.getContext(), R.layout.item_slide,null);
vh = new ViewHolder(convertView);
convertView.setTag(vh);
}else{
vh = (ViewHolder) convertView.getTag();
}
vh.content.setText(list.get(position));
vh.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
notifyDataSetChanged();
}
});
vh.container.requestLayout();
return convertView;
}
class ViewHolder{
private TextView content;
private TextView delete;
private SlideDelete container;
public ViewHolder(View v){
container = (SlideDelete) v.findViewById(R.id.container);
content = (TextView) v.findViewById(R.id.content);
delete = (TextView) v.findViewById(R.id.delete);
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
ListView>
RelativeLayout>
item_slide.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.movedelete.SlideDelete
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="80dp">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#33000000"
android:text="第0个条目"
android:textColor="#fff"
android:textSize="20sp" />
<TextView
android:id="@+id/delete"
android:layout_width="80dp"
android:layout_height="match_parent"
android:background="#f00"
android:gravity="center"
android:text="删除"
android:textColor="#fff"
android:textSize="20sp" />
com.example.movedelete.SlideDelete>
LinearLayout>