亲宝软件园·资讯

展开

Android滑动删除 Android实现滑动删除操作(PopupWindow)

liDB 人气:0
想了解Android实现滑动删除操作(PopupWindow)的相关内容吗,liDB在本文为您仔细讲解Android滑动删除的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ListView,PopupWindow,滑动删除,下面大家一起来学习吧。

参考Android仿腾讯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);
 }
} 

通过这个例子学习到:
 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的使用)。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论