您的位置:首页 > 移动开发 > Android开发

QQ侧滑删除分享(工具类)

2015-11-24 00:00 441 查看
摘要: 分享一个工具类
package com.qudoulicai.www.qqlistview.QQlistview;import android.content.Context;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.view.MotionEvent;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.LinearLayout;import android.widget.ListView;public class QQlistview extends ListView {private int mScreenWidth;  // 屏幕宽度private int mDownX;          // 按下点的x值private int mDownY;          // 按下点的y值private int mDeleteBtnWidth;// 删除按钮的宽度private boolean isDeleteShown; // 删除按钮是否正在显示private ViewGroup mPointChild; // 当前处理的itemprivate LinearLayout.LayoutParams mLayoutParams;   // 当前处理的item的LayoutParamspublic QQlistview(Context context, AttributeSet attrs) {this(context, attrs, 0);}public QQlistview(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;}@Overridepublic 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();// 获取当前点的itemmPointChild = (ViewGroup) getChildAt(pointToPosition(mDownX, mDownY)- getFirstVisiblePosition());// 获取删除按钮的宽度mDeleteBtnWidth = mPointChild.getChildAt(1).getLayoutParams().width;mLayoutParams = (LinearLayout.LayoutParams) mPointChild.getChildAt(0).getLayoutParams();// 为什么要重新设置layout_width 等于屏幕宽度// 因为match_parent时,不管你怎么滑,都不会显示删除按钮// why? 因为match_parent时,ViewGroup就不去布局剩下的viewmLayoutParams.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;}// 重新设置leftMarginmLayoutParams.leftMargin = scroll+10;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;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;}}//非原创原创地址太偏。。搬运过来
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息