Android - ListView slide left/right like Samsung contact ListView

前端 未结 3 587
青春惊慌失措
青春惊慌失措 2020-12-02 06:44

I am developing an application, and I need a ListView like conctact ListView of my Samsung Galaxy S:

When I slide my finger to the right I can send message

3条回答
  •  孤城傲影
    2020-12-02 07:17

    What you might what to do here is create a new view especially for the list view (call it ListViewFlinger or something). Then in this view, override its onTouchEvent method and place some code in there to determine a slide gesture. Once you have the slide gesture, fire a onSlideComplete event (you'll have to make that listener) an voialla, you a ListView with slide activated content.

    float historicX = Float.NaN, historicY = Float.NaN;
    static final TRIGGER_DELTA = 50; // Number of pixels to travel till trigger
    
    @Override public boolean onTouchEvent(MotionEvent e) {
    
        switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            historicX = e.getX();
            historicY = e.getY();
            break;
        case MotionEvent.ACTION_UP:
            if (e.getX() - historicX > -TRIGGER_DELTA) {
                onSlideComplete(Direction.LEFT);
                return true;
            }
            else if (e.getX() - historicX > TRIGGER_DELTA)  {
                onSlideComplete(Direction.RIGHT);
                return true;
            } break;
        default:
            return super.onTouchEvent(e);
        }
    }
    
    enum Direction {
        LEFT, RIGHT;
    }
    
    interface OnSlideCompleteListener {
        void onSlideComplete(Direction dir);
    }
    

提交回复
热议问题