ViewDragHelper的使用–滑动删除
*item布局文件view_slide_remove.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.andriodbus.slideremoveview.SlideRemoveView
android:id="@+id/srv"
android:layout_width="match_parent"
android:layout_height="80dp">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#33ffffff"
android:gravity="center"
android:text="内容"/>
<TextView
android:id="@+id/delete"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#ff0000"
android:gravity="center"
android:text="删除"/>
</com.itheima.andriodbus.slideremoveview.SlideRemoveView>
</LinearLayout>
*自定义滑动删除的代码–SlideRemoveView
import android.content.Context;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
/*
* @项目名: SlideRemoveView
* @包名:com.andriodbus.slideremoveview
* @文件名: SlideRemoveView
* @创建者: Administrator
* @创建时间: 2016/8/21 0021 下午 10:59
* @描述:viewDragHelper的使用
*/
public class SlideRemoveView extends ViewGroup {
private ViewDragHelper mViewDragHelper;
private View mLeftChild;
private View mRightChild;
private ViewDragHelper.Callback cb = new ViewDragHelper.Callback() {
/**
* Called when the captured view's position changes as the result of a drag or settle.
*
* @param changedView View whose position changed
* @param left New X coordinate of the left edge of the view
* @param top New Y coordinate of the top edge of the view
* @param dx Change in X position from the last call
* @param dy Change in Y position from the last call
*/
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
if (changedView == mLeftChild) { //左边孩子位置变化时,布局右边孩子的位置,以实现联动
mRightChild.layout(mRightChild.getLeft() + dx,
mRightChild.getTop(),
mRightChild.getRight() + dx,
mRightChild.getBottom());
} else if (changedView == mRightChild) {
mLeftChild.layout(mLeftChild.getLeft() + dx,
mLeftChild.getTop(),
mLeftChild.getRight() + dx,
mLeftChild.getBottom());
}
}
/**
* 手指松开时调用
* @param releasedChild
* @param xvel
* @param yvel
*/
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
//引起右边孩子最终可能位置的关键
int keyPosition = getWidth() - mRightChild.getWidth() / 2;
if (mRightChild.getLeft() < keyPosition) { //右边孩子中心已进入右边孩子内时
//缓慢回滚到最终位置
mViewDragHelper.smoothSlideViewTo(mRightChild,
getWidth() - mRightChild.getWidth(),
0);
//触发重新绘制
invalidate();
} else {
mViewDragHelper.smoothSlideViewTo(mRightChild, getWidth(), 0);
invalidate();
}
}
@Override
public boolean tryCaptureView(View child, int pointerId) {
return true; //true if capture should be allowed
}
/**
* Restrict the motion of the dragged child view along the horizontal axis.
* @param child 指定要拖动的孩子
* @param left left Attempted motion along the X axis
* @param dx Proposed change in position for left
* @return
*/
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if (child == mLeftChild) { //拖动左边孩子时,左边孩子能够拖动的范围
if (left < -mRightChild.getMeasuredWidth()) {
left = -mRightChild.getMeasuredWidth();
} else if (left > 0) {
left = 0;
}
} else if (child == mRightChild) { //拖动右边孩子时,右边孩子能够拖动的范围
if (left < getWidth() - mRightChild.getMeasuredWidth()) {
left = getWidth() - mRightChild.getMeasuredWidth();
} else if (left > getWidth()) {
left = getWidth();
}
}
return left;
}
};
public SlideRemoveView(Context context) {
this(context, null);
}
public SlideRemoveView(Context context, AttributeSet attrs) {
super(context, attrs);
//创建viewDragHelper对象
mViewDragHelper= ViewDragHelper.create(this, cb);
}
/**
* 重新绘制,更新位置
* @param
*/
@Override
public void computeScroll() {
if (mViewDragHelper.continueSettling(true)) {
//继续绘制
invalidate();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//由于布局中为EXACTL模式,可以直接super
//测量孩子
measureChildren(widthMeasureSpec, heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//获取孩子
mLeftChild = getChildAt(0);
mRightChild = getChildAt(1);
//布局孩子
mLeftChild.layout(0, 0, getWidth(), mLeftChild.getMeasuredHeight());
mRightChild.layout(getWidth(),
0,
getWidth() + mRightChild.getMeasuredWidth(),
mRightChild.getMeasuredHeight());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//将拖拽事件交给viewDragHeLper处理
mViewDragHelper.processTouchEvent(event);
return true;
}
}
*MainActivity处理滑动点击删除按钮删除操作
package com.andriodbus.slideremoveview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private ArrayList<String> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initEvent();
initData();
}
private void initView() {
mListView = (ListView) findViewById(R.id.lv);
}
private void initEvent() {
}
private void initData() {
mList = new ArrayList();
for (int i = 0; i < 30; i++) {
mList.add("条目" + i);
}
myAdapter adapter = new myAdapter();
mListView.setAdapter(adapter);
}
static class ViewHolder {
TextView content;
TextView delete;
public ViewHolder(View root) {
content = (TextView) root.findViewById(R.id.content);
delete = (TextView) root.findViewById(R.id.delete);
}
}
class myAdapter extends BaseAdapter {
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.view_slide_remove, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String s = mList.get(position);
holder.content.setText(s);
//点击删除,删除该条目
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//拿到父容器进行重新布局
holder.delete.getParent().requestLayout();
//从数据集中删除,更新UI
mList.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
}
}
*MainActivity的布局里是一个listview,这里就不贴代码了