Remove item listview with Slide - Like Gmail

前端 未结 4 1427
北恋
北恋 2020-12-23 02:51

I am developing an application with a shop list in a listview. I need that when I swipe the item of listview to the right(or left), this item should get deleted

4条回答
  •  再見小時候
    2020-12-23 03:00

    I made an answer using what macloving wrote. For now it is working, but it is only work if all your childs have the same height.

    listView.setOnTouchListener(new View.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();
                        return false;
    
                    case MotionEvent.ACTION_UP:
                        if (listView.getChildAt(0) != null) {
                            int heightOfEachItem = haveListView.getChildAt(0).getHeight();
                            int heightOfFirstItem = -haveListView.getChildAt(0).getTop() + haveListView.getFirstVisiblePosition()*heightOfEachItem;
                            //IF YOU HAVE CHILDS IN LIST VIEW YOU START COUNTING
                            //listView.getChildAt(0).getTop() will see top of child showed in screen
                            //Dividing by height of view, you get how many views are not in the screen
                            //It needs to be Math.ceil in this case because it sometimes only shows part of last view
                            final int firstPosition = (int) Math.ceil(heightOfFirstItem / heightOfEachItem); // This is the same as child #0
    
                            //Here you get your List position, use historic Y to get where the user went first
                            final int wantedPosition = (int) Math.floor((historicY - haveListView.getChildAt(0).getTop()) / heightOfEachItem) + firstPosition;
                            //Here you get the actually position in the screen
                            final int wantedChild = wantedPosition - firstPosition;
                            //Depending on delta, go right or left
                            if (event.getX() - historicX < -DELTA) {
                                //If something went wrong, we stop it now
                                if (wantedChild < 0 || wantedChild >= List.size()|| wantedChild >= listView.getChildCount()) {
    
                                    return true;
                                }
                                //Start animation with 500 miliseconds of time
                                listView.getChildAt(wantedChild).startAnimation(outToLeftAnimation(500));
                                //after 500 miliseconds remove from List the item and update the adapter.
                                new java.util.Timer().schedule(
                                        new java.util.TimerTask() {
                                            @Override
                                            public void run() {
                                                List.remove(wantedPosition);
                                                updateAdapter();
                                            }
                                        },
                                        500
                                );
                                return true;
    
                            } else if (event.getX() - historicX > DELTA) {
                                //If something went wrong, we stop it now
                                if (wantedChild < 0 || wantedChild >= List.size() || wantedChild >= listView.getChildCount()) {
    
                                    return true;
                                }
                                //Start animation with 500 miliseconds of time
                                listView.getChildAt(wantedChild).startAnimation(outToRightAnimation(500));
                                //after 500 miliseconds remove from List the item and update the adapter.
                                new java.util.Timer().schedule(
                                        new java.util.TimerTask() {
                                            @Override
                                            public void run() {
                                                List.remove(wantedPosition);
                                                updateAdapter();
                                            }
                                        },
                                        500
                                );
                                return true;
    
                            }
                        }
                        return true;
                    default:
                        return false;
                }
            }
        });
    

    The animations have this function:

    private Animation outToLeftAnimation(int duration) {
        Animation outtoLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        outtoLeft.setDuration(duration);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
    }
    
    private Animation outToRightAnimation(int duration) {
        Animation outtoRight = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, +1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        outtoRight.setDuration(duration);
        outtoRight.setInterpolator(new AccelerateInterpolator());
        return outtoRight;
    }
    

    I am trying this, and until now i haven't seen errors, if someone could try as well would be good.

提交回复
热议问题