码迷,mamicode.com
首页 > Windows程序 > 详细

ListView + PopupWindow实现滑动删除

时间:2016-07-13 13:31:11      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

 

原文:ListView滑动删除 ,仿腾讯QQ(鸿洋_)

文章实现的功能是:在ListView的Item上从右向左滑时,出现删除按钮,点击删除按钮把Item删除。

 

看过文章后,感觉没有必要把dispatchTouchEvent()和onTouchEvent()两个方法都重写,只要重写onTouchEvent就好了。于是对代码作了一些调整:

技术分享
public class MyListView extends ListView {
    private static final String TAG = "MyListView";
    private int mTouchSlop;
    private int mXDown;
    private int mYDown;
    private int mCurrentPosition;
    private View mCurrentView;
    private PopupWindow mPopupWindow;
    private LayoutInflater mInflater;
    private boolean isSliding = false;
    // 为删除按钮提供一个回调接口
    private DelButtonClickListener mListener;
    private Button mDelBtn;
    private int mPopupWindowHeight;
    private int mPopupWindowWidth;

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mInflater = LayoutInflater.from(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

        View view = mInflater.inflate(R.layout.delete_btn, null);
        mDelBtn = (Button) view.findViewById(R.id.id_item_btn);
        mPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        // 如果需要通过点击PopupWindow之外的地方使其消失,则需要setFocusable(true).
        mPopupWindow.setFocusable(true);
        // Android 6.0以前的版本需要setBackgroundDrawable(),
        // 才能实现通过点击PopupWindow之外的地方使其消失的功能。
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
        // 先调用下measure,否则拿不到宽和高
        mPopupWindow.getContentView().measure(0, 0);
        mPopupWindowHeight = mPopupWindow.getContentView().getMeasuredHeight();
        mPopupWindowWidth = mPopupWindow.getContentView().getMeasuredWidth();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        int x = (int) ev.getX();
        int y = (int) ev.getY();

        switch (action){
            case MotionEvent.ACTION_DOWN:
                isSliding = false;
                mXDown = x;
                mYDown = y;
                mCurrentPosition = pointToPosition(mXDown, mYDown);
                View view = getChildAt(mCurrentPosition - getFirstVisiblePosition());
                mCurrentView = view;
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = x - mXDown;
                int dy = y - mYDown;

                Log.d(TAG, "mTouchSlop = " + mTouchSlop + ", dx = " + dx + ", dy = " + dy);

                if(mXDown > x && Math.abs(dx) > mTouchSlop && Math.abs(dy) < mTouchSlop){
                    Log.d(TAG, "isSliding");
                    isSliding = true;
                    int[] location = new int[2];
                    mCurrentView.getLocationOnScreen(location);
                    mPopupWindow.setAnimationStyle(R.style.popwindow_delete_btn_anim_style);
                    mPopupWindow.update();
                    Log.d(TAG, "Height: " + mCurrentView.getHeight() + "," + mPopupWindow.getHeight());
                    mPopupWindow.showAtLocation(mCurrentView, Gravity.NO_GRAVITY,
                            location[0] + mCurrentView.getWidth(),
                            location[1] + mCurrentView.getHeight() / 2 - mPopupWindowHeight / 2);
                    mDelBtn.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mListener.clickHappend(mCurrentPosition);
                            mPopupWindow.dismiss();
                        }
                    });
                }
            case MotionEvent.ACTION_UP:
                // isSliding 如果这里恢复为false,则后面会执行super.onTouchEvent事件,
                // 而AbsListView的onTouchEvent调用了onTouchUp方法,在onTouchUp方法中有可能执行
                // performClick.run() --> performItemClick() --> super.performItemClick
                // --> mOnItemClickListener.onItemClick,这样最终触发Item的点击。
                // 因此此处依旧保持isSliding为true的状态,而在ACTION_DOWN事件中恢复isSliding为false,
                // 毕竟每个事件都以ACTION_DOWN开始。
                //isSliding = false;
        }

        if(isSliding){
            return true;
        }

        return super.onTouchEvent(ev);
    }

    public void setDelButtonClickListener(DelButtonClickListener listener){
        mListener = listener;
    }

    interface DelButtonClickListener{
        public void clickHappend(int position);
    }
}
MyListView.java

 

通过这个例子学习到:

1、ListView的Item点击事件的触发过程:

自定义ListView的onTouchEvent()  ---调用super.onTouchEvent()---> AbsListView.onTouchEvent() ---MotionEvent.ACTION_UP---> AbsListView.onTouchUp()

---(有可能)调用performClick.run()---> AbsListView.PerformClick.run() ---调用performItemClick()---> AbsListView.performItemClick()

---(有可能)调用super.performItemClick()---> AdapterView.performItemClick() ---mOnItemClickListener.onItemClick---> OnItemClickListener.onItemClick()

也就是Item的点击事件是在MotionEvent.ACTION_UP事件完成的,这样在自定义ListView的onTouchEvent()中,对MotionEvent.ACTION_UP直接return true消费掉事件,而不要调用super.onTouchEvent。这样就避免了删除按钮与Item点击事件的冲突。

 

2、PopupWindow--通过点击PopupWindow之外的地方使其消失

a、需要调用setFocusable()方法(PopupWindow中showAtLocation() --> createPopupLayoutParams() --> computeFlags() --> 设置FLAG_NOT_FOCUSABLE);

b、Android 6.0以前的版本需要setBackgroundDrawable()(具体原因见:PopupWindow的使用)。

 

ListView + PopupWindow实现滑动删除

标签:

原文地址:http://www.cnblogs.com/yarightok/p/5666127.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!