设为首页 加入收藏

TOP

listview侧滑删除(一)
2017-10-13 10:35:45 】 浏览:5444
Tags:listview 删除

自定义Listview,向左滑动,右边刚好显示删除按钮:

public class SlideListView extends ListView {
private int mScreenWidth; // 屏幕宽度
private int mDownX; // 按下点的x值
private int mDownY; // 按下点的y值
private int mDeleteBtnWidth;// 删除按钮的宽度

private boolean isDeleteShown; // 删除按钮是否正在显示

private ViewGroup mPointChild; // 当前处理的item
private LinearLayout.LayoutParams mLayoutParams; // 当前处理的item的LayoutParams

public SlideListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public SlideListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

// 获取屏幕宽度
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
mScreenWidth = dm.widthPixels;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
performActionDown(ev);
break;
case MotionEvent.ACTION_MOVE:
return performActionMove(ev);
case MotionEvent.ACTION_UP:
performActionUp();
break;
}

return super.onTouchEvent(ev);
}

// 处理action_down事件
private void performActionDown(MotionEvent ev) {
if (isDeleteShown) {
turnToNormal();
}

mDownX = (int) ev.getX();
mDownY = (int) ev.getY();
// 获取当前点的item
mPointChild = (ViewGroup) getChildAt(pointToPosition(mDownX, mDownY)
- getFirstVisiblePosition());
// 获取删除按钮的宽度
mDeleteBtnWidth = mPointChild.getChildAt(1).getLayoutParams().width;
mLayoutParams = (LinearLayout.LayoutParams) mPointChild.getChildAt(0)
.getLayoutParams();
mLayoutParams.width = mScreenWidth;
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
}

// 处理action_move事件
private boolean performActionMove(MotionEvent ev) {
int nowX = (int) ev.getX();
int nowY = (int) ev.getY();
if (Math.abs(nowX - mDownX) > Math.abs(nowY - mDownY)) {
// 如果向左滑动
if (nowX < mDownX) {
// 计算要偏移的距离
int scroll = (nowX - mDownX) / 2;
// 如果大于了删除按钮的宽度, 则最大为删除按钮的宽度
if (-scroll >= mDeleteBtnWidth) {
scroll = -mDeleteBtnWidth;
}
// 重新设置leftMargin
mLayoutParams.leftMargin = scroll;
// mLayoutParams.leftMargin = scroll*2;
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
}
// return true;
}
return super.onTouchEvent(ev);
}

// 处理action_up事件
private void performActionUp() {
// 偏移量大于button的一半,则显示button
// 否则恢复默认
if (-mLayoutParams.leftMargin >= mDeleteBtnWidth / 2) {
mLayoutParams.leftMargin = -mDeleteBtnWidth;
// mLayoutParams.leftMargin = -mDeleteBtnWidth * 2;
isDeleteShown = true;
} else {
turnToNormal();
}

mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
}

/**
* 变为正常状态
*/
public void turnToNormal() {
mLayoutParams.leftMargin = 0;
mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);
isDeleteShown = false;
}

/**
* 当前是否可点击
*
* @return 是否可点击
*/
public boolean canClick() {
return !isDeleteShown;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//与scrollview嵌套需要重新计算高度
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}


适配器:
public class ListViewSlideAdapter extends BaseAdapter {

private ArrayList<String> bulbList;
private Activity context;
private OnClickListenerEditOrDelete onClickListenerEditOrDelete;

public ListViewSlideAdapter(Activity context, ArrayList<String> bulbList) {
        this.bulbList = bulbList;
th
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Github如何删除repository(仓库) 下一篇为什么 Android Studio 工程文件..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目