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

看過(guò)文章后,感覺(jué)沒(méi)有必要把dispatchTouchEvent()和onTouchEvent()兩個(gè)方法都重寫(xiě),只要重寫(xiě)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);
// 如果需要通過(guò)點(diǎn)擊PopupWindow之外的地方使其消失,則需要setFocusable(true).
mPopupWindow.setFocusable(true);
// Android 6.0以前的版本需要setBackgroundDrawable(),
// 才能實(shí)現(xiàn)通過(guò)點(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開(kāi)始。
//isSliding = false;
}
if(isSliding){
return true;
}
return super.onTouchEvent(ev);
}
public void setDelButtonClickListener(DelButtonClickListener listener){
mListener = listener;
}
interface DelButtonClickListener{
public void clickHappend(int position);
}
}
通過(guò)這個(gè)例子學(xué)習(xí)到:
1、ListView的Item點(diǎn)擊事件的觸發(fā)過(guò)程:
自定義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--通過(guò)點(diǎn)擊PopupWindow之外的地方使其消失
a、需要調(diào)用setFocusable()方法(PopupWindow中showAtLocation() --> createPopupLayoutParams() --> computeFlags() --> 設(shè)置FLAG_NOT_FOCUSABLE);
b、Android 6.0以前的版本需要setBackgroundDrawable()(具體原因見(jiàn):PopupWindow的使用)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android學(xué)習(xí)之使用SharedPreferences存儲(chǔ)應(yīng)用程序數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)之使用SharedPreferences保存應(yīng)用程序數(shù)據(jù)的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果
這篇文章主要介紹了Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果,實(shí)現(xiàn)思路是先分別實(shí)現(xiàn)閃屏、注冊(cè)界面、登錄界面的活動(dòng),再用Intent將相關(guān)的活動(dòng)連接起來(lái),實(shí)現(xiàn)不同活動(dòng)之間的跳轉(zhuǎn),對(duì)android 實(shí)現(xiàn)閃屏和界面切換感興趣的朋友一起看看吧2016-11-11
Spi機(jī)制在Android開(kāi)發(fā)的應(yīng)用示例詳解
這篇文章主要為大家介紹了Spi機(jī)制在Android開(kāi)發(fā)的應(yīng)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android 8.0升級(jí)不跳轉(zhuǎn)應(yīng)用安裝頁(yè)面的解決方法
這篇文章主要為大家詳細(xì)介紹了Android 8.0升級(jí)不跳轉(zhuǎn)應(yīng)用安裝頁(yè)面的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android ViewPager實(shí)現(xiàn)滑動(dòng)指示條功能
這篇文章主要介紹了Android-ViewPager實(shí)現(xiàn)滑動(dòng)指示條功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Android實(shí)現(xiàn)京東App分類(lèi)頁(yè)面效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)京東App分類(lèi)頁(yè)面效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
不依賴于Activity的Android全局懸浮窗的實(shí)現(xiàn)
在Android應(yīng)用開(kāi)發(fā)中,經(jīng)常要遇到做全局懸浮窗的效果,本文的內(nèi)容主要是如何不依賴于Activity的全局懸浮窗的實(shí)現(xiàn)及原理,有需要的可以參考。2016-07-07
Android實(shí)現(xiàn)一個(gè)簡(jiǎn)單的單詞本
大家好,本篇文章主要講的是Android實(shí)現(xiàn)一個(gè)簡(jiǎn)單的單詞本,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01

