亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實(shí)現(xiàn)滑動(dòng)刪除操作(PopupWindow)

 更新時(shí)間:2016年07月13日 15:07:43   作者:liDB  
這篇文章主要介紹了Android ListView結(jié)合PopupWindow實(shí)現(xiàn)滑動(dòng)刪除的相關(guān)資料,需要的朋友可以參考下

參考Android仿騰訊QQ實(shí)現(xiàn)滑動(dòng)刪除這篇文章進(jìn)行學(xué)習(xí),文章實(shí)現(xiàn)的功能是:在ListView的Item上從右向左滑時(shí),出現(xiàn)刪除按鈕,點(diǎn)擊刪除按鈕把Item刪除,效果

看過文章后,感覺沒有必要把dispatchTouchEvent()和onTouchEvent()兩個(gè)方法都重寫,只要重寫onTouchEvent就好了。于是對(duì)代碼作了一些調(diào)整: 

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;
 // 為刪除按鈕提供一個(gè)回調(diào)接口
 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);
  // 如果需要通過點(diǎn)擊PopupWindow之外的地方使其消失,則需要setFocusable(true).
  mPopupWindow.setFocusable(true);
  // Android 6.0以前的版本需要setBackgroundDrawable(),
  // 才能實(shí)現(xiàn)通過點(diǎn)擊PopupWindow之外的地方使其消失的功能。
  mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
  // 先調(diào)用下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 如果這里恢復(fù)為false,則后面會(huì)執(zhí)行super.onTouchEvent事件,
    // 而AbsListView的onTouchEvent調(diào)用了onTouchUp方法,在onTouchUp方法中有可能執(zhí)行
    // performClick.run() --> performItemClick() --> super.performItemClick
    // --> mOnItemClickListener.onItemClick,這樣最終觸發(fā)Item的點(diǎn)擊。
    // 因此此處依舊保持isSliding為true的狀態(tài),而在ACTION_DOWN事件中恢復(fù)isSliding為false,
    // 畢竟每個(gè)事件都以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);
 }
} 

通過這個(gè)例子學(xué)習(xí)到:
 1、ListView的Item點(diǎn)擊事件的觸發(fā)過程:
 自定義ListView的onTouchEvent()  ---調(diào)用super.onTouchEvent()---> AbsListView.onTouchEvent() ---MotionEvent.ACTION_UP---> AbsListView.onTouchUp() 
---(有可能)調(diào)用performClick.run()---> AbsListView.PerformClick.run() ---調(diào)用performItemClick()---> AbsListView.performItemClick()

---(有可能)調(diào)用super.performItemClick()---> AdapterView.performItemClick() ---mOnItemClickListener.onItemClick---> OnItemClickListener.onItemClick() 
也就是Item的點(diǎn)擊事件是在MotionEvent.ACTION_UP事件完成的,這樣在自定義ListView的onTouchEvent()中,對(duì)MotionEvent.ACTION_UP直接return true消費(fèi)掉事件,而不要調(diào)用super.onTouchEvent。這樣就避免了刪除按鈕與Item點(diǎn)擊事件的沖突。 

2、PopupWindow--通過點(diǎn)擊PopupWindow之外的地方使其消失 
a、需要調(diào)用setFocusable()方法(PopupWindow中showAtLocation() --> createPopupLayoutParams() --> computeFlags() --> 設(shè)置FLAG_NOT_FOCUSABLE); 
b、Android 6.0以前的版本需要setBackgroundDrawable()(具體原因見:PopupWindow的使用)。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論