原文: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

相关文章:

  • 2021-10-10
  • 2021-10-09
  • 2022-12-23
  • 2021-06-03
  • 2022-12-23
  • 2021-04-06
  • 2022-12-23
  • 2021-04-26
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2022-02-22
相关资源
相似解决方案