Resource for Android Slight Left/Right Slide action on listview

前端 未结 4 1407
耶瑟儿~
耶瑟儿~ 2021-02-03 13:27

I am not sure if this question has been asked here or not. I look for a way to simulate the slide action in listview of Android. Similar to that of Samsung Galaxy/Nexus contact

4条回答
  •  梦毁少年i
    2021-02-03 14:31

    This is how I realize this action. We have a ListView lvSimple and we add onTouchListener to our lvSimple. This is my working code.

    float historicX = Float.NaN, historicY = Float.NaN;
    static final int DELTA = 50;
    enum Direction {LEFT, RIGHT;}
    ...
    ListView lvSimple = (ListView) findViewById(R.id.linLayout);
    ...
    lvSimple.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            // TODO Auto-generated method stub
            switch (event.getAction()) 
            {
                case MotionEvent.ACTION_DOWN:
                historicX = event.getX();
                historicY = event.getY();
                break;
    
                case MotionEvent.ACTION_UP:
                if (event.getX() - historicX < -DELTA) 
                {
                    FunctionDeleteRowWhenSlidingLeft();
                    return true;
                }
                else if (event.getX() - historicX > DELTA)  
                {
                    FunctionDeleteRowWhenSlidingRight();
                    return true;
                } break;
                default: return false;
            }
            return false;
        }
    });
    

    where function FunctionDeleteRowWhenSlidingLeft() is calling when when we sliding to the left, FunctionDeleteRowWhenSlidingRight - to the right respectively. In this function you need paste code for animation.

提交回复
热议问题