import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
/**
* 可修改ListItemView extends 任何布局
* 父窗体为RelativeLayout
* @author Administrator
*
*/
public class ListItemView extends LinearLayout implements OnTouchListener{
private float x;//item被触摸时x起点
private int l;//x轴上滑动的距离
private boolean isShow = false;//底部
private boolean left = false;
private boolean right = false;
private android.widget.RelativeLayout.LayoutParams layoutParams; //父窗体为RelativeLayout可根据需要修改
private int longth = 100;//滑动结束时滑动的距离
private int min = 10;//屏蔽滑动小于10的情况算为点击事件
private int max = 30;//滑动结束时滑动的距离大于该值滑动完成否则取消滑动
public ListItemView(Context context) {
super(context);
setOnTouchListener(this);
}
public ListItemView(Context context, AttributeSet attrs) {
super(context, attrs);
setOnTouchListener(this);
}
public ListItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOnTouchListener(this);
}
public ListItemView(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams = (android.widget.RelativeLayout.LayoutParams) getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
break;
case MotionEvent.ACTION_MOVE:
l = (int) (x - event.getX());
if (l > 0 && !isShow) {
layoutParams.setMargins(-l, 0, l, 0);
requestLayout();
left = true;
} else if (l < 0 && isShow && l > -longth) {
layoutParams.setMargins(-longth - l, 0, longth + l, 0);
requestLayout();
right = true;
}
break;
case MotionEvent.ACTION_UP:
if (!isShow && left) {
if(l < min){
return false;
}
if (l > max) {
layoutParams.setMargins(-longth, 0, longth, 0);
requestLayout();
isShow = true;
left = false;
}else{
layoutParams.setMargins(0, 0, 0, 0);
requestLayout();
}
return true;
}
if (isShow && right) {
if(l > -min){
return false;
}
if(l < -max){
layoutParams.setMargins(0, 0, 0, 0);
requestLayout();
isShow = false;
right = false;
}else{
layoutParams.setMargins(-longth, 0, longth, 0);
requestLayout();
}
return true;
}
break;
case MotionEvent.ACTION_CANCEL:
if (!isShow && left) {
if (l > max) {
layoutParams.setMargins(-longth, 0, longth, 0);
requestLayout();
isShow = true;
left = false;
}else{
layoutParams.setMargins(0, 0, 0, 0);
requestLayout();
}
} else if (isShow && right) {
if(l < -max){
layoutParams.setMargins(0, 0, 0, 0);
requestLayout();
isShow = false;
right = false;
}else{
layoutParams.setMargins(-longth, 0, longth, 0);
requestLayout();
}
}
break;
default:
break;
}
return false;
}
}
用法例子
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:baselineAligned="false"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_dele"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_alignParentRight="true"
android:text="删除" />
<com.bs.rrz.view.ListItemView
android:id="@+id/ll_msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/pink"
android:orientation="vertical"
android:padding="8dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textColor="@color/black"
android:textSize="12dp" />
</LinearLayout>
</com.bs.rrz.view.ListItemView>
</RelativeLayout>