SoFunction
Updated on 2025-03-03

Android program development ListView and PopupWindow realize the function of sliding from left to right to delete

The function implemented by the article is: When you swipe from right to left on the Item of ListView, the delete button appears, and click the delete button to delete the Item.

After reading the article, I feel that there is no need to rewrite both dispatchTouchEvent() and onTouchEvent() methods, just rewrite onTouchEvent. So some adjustments were made to the code:

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;
// Provide a callback interface for the delete buttonprivate DelButtonClickListener mListener;
private Button mDelBtn;
private int mPopupWindowHeight;
private int mPopupWindowWidth;
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
mInflater = (context);
mTouchSlop = (context).getScaledTouchSlop();
View view = (.delete_btn, null);
mDelBtn = (Button) (.id_item_btn);
mPopupWindow = new PopupWindow(view, .WRAP_CONTENT,
.WRAP_CONTENT);
// If you need to make it disappear by clicking outside of PopupWindow, you need to setFocusable(true).(true);
// Versions before Android 6.0 require setBackgroundDrawable(),// Only by clicking on PopupWindow can it disappear.(new ColorDrawable(0));
// Call measure first, otherwise you won't get the width and height().measure(0, 0);
mPopupWindowHeight = ().getMeasuredHeight();
mPopupWindowWidth = ().getMeasuredWidth();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = ();
int x = (int) ();
int y = (int) ();
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;
(TAG, "mTouchSlop = " + mTouchSlop + ", dx = " + dx + ", dy = " + dy);
if(mXDown > x && (dx) > mTouchSlop && (dy) < mTouchSlop){
(TAG, "isSliding");
isSliding = true;
int[] location = new int[2];
(location);
(.popwindow_delete_btn_anim_style);
();
(TAG, "Height: " + () + "," + ());
(mCurrentView, Gravity.NO_GRAVITY,
location[0] + (),
location[1] + () / 2 - mPopupWindowHeight / 2);
(new OnClickListener() {
@Override
public void onClick(View v) {
(mCurrentPosition);
();
}
});
}
case MotionEvent.ACTION_UP:
// isSliding If it is restored to false here, an event will be executed later.// AbsListView's onTouchEvent calls the onTouchUp method, which may be executed in the onTouchUp method// () --> performItemClick() --> 
// --> , which eventually triggers the click of Item.// Therefore, the state of isSliding as true is still maintained here, and isSliding as false is restored in the ACTION_DOWN event.// After all, every event starts with ACTION_DOWN.//isSliding = false;
}
if(isSliding){
return true;
}
return (ev);
}
public void setDelButtonClickListener(DelButtonClickListener listener){
mListener = listener;
}
interface DelButtonClickListener{
public void clickHappend(int position);
}
}

From this example, I learned:

1. The triggering process of ListView's Item click event:

Customize the onTouchEvent() of ListView---Call()---> () ---MotionEvent.ACTION_UP---> ()

---(Possible) Call()---> () ---Call performItemClick()---> ()

---(Possible) Call()----> () ------> ()

That is, the click event of Item is completed in the MotionEvent.ACTION_UP event. In the onTouchEvent() of the custom ListView, the event is consumed directly by return true to MotionEvent.ACTION_UP, and do not call it. This avoids conflict between the delete button and the Item click event.

2. PopupWindow--Change it by clicking outside PopupWindow

a. You need to call the setFocusable() method;

b. Versions before Android 6.0 require setBackgroundDrawable() (see the use of PopupWindow for specific reasons).

The above is the Android program development ListView and PopupWindow that I introduced to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!