Showing a delete button on swipe in a listview for Android

前端 未结 1 1857
天命终不由人
天命终不由人 2020-12-29 15:21

Expanding on another Stackoverflow question, I\'ve implemented some gesture detection code so that I can detect when a row in my listview (which is in a FrameLayout) has bee

相关标签:
1条回答
  • 2020-12-29 15:59

    I implemented something like this in my app once. The way I did it:

    public class MyGestureDetector extends SimpleOnGestureListener {
        private ListView list;
    
        public MyGestureDetector(ListView list) {
            this.list = list;
        }
    
        //CONDITIONS ARE TYPICALLY VELOCITY OR DISTANCE    
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (INSERT_CONDITIONS_HERE)
                if (showDeleteButton(e1))
                    return true;
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    
        private boolean showDeleteButton(MotionEvent e1) {
            int pos = list.pointToPosition((int)e1.getX(), (int)e1.getY());
            return showDeleteButton(pos);
        }
    
        private boolean showDeleteButton(int pos) {
            View child = list.getChildAt(pos);
            if (child != null){
                Button delete = (Button) child.findViewById(R.id.delete_button_id);
                if (delete != null)
                    if (delete.getVisibility() == View.INVISIBLE)
                        delete.setVisibility(View.VISIBLE);
                    else
                        delete.setVisibility(View.INVISIBLE);
                return true;
            }
            return false;
        }
    }
    

    This worked for me, hope you'll get it to work or that it at least gives you some hint.

    0 讨论(0)
提交回复
热议问题