0

I have a list view and I want to make it so I can swipe each item to reveal a delete button:

enter image description here

I've figured out how to recognise the swipe event, I am using this code (listItem is of type View):

listItem.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            switch (event.getAction()) 
            {
                case MotionEvent.ACTION_DOWN:
                    _xSwipe1 = event.getX();
                    break;

                case MotionEvent.ACTION_UP:
                    _xSwipe2 = event.getX();

                    float deltaX = _xSwipe2 - _xSwipe1;

                    if (deltaX < 0) 
                    {
                        Log.e("SWIPE", "Right to Left swipe");
                    }

                    else if (deltaX >0)
                    {
                        Log.e("SWIPE", "Left to right swipe");
                    }

                    break;
            }

            return false;
        }
    });

And when I swipe, I can see in the logs that the swipe event is being recognised.

However, I'm not sure how to physically make the list item start disappearing to the left?

Any help would be greatly appreciated.

b85411
  • 9,420
  • 15
  • 65
  • 119
  • only swipe is not working? – Aditya Vyas-Lakhan May 31 '16 at 03:33
  • The swipe event is being recognised, I just don't know what to do next. How do I actually make the list view item start shooting towards the left (if I do a right-to-left swipe)? – b85411 May 31 '16 at 03:36
  • what is the exact problem?and what are you trying to achieve? – Aditya Vyas-Lakhan May 31 '16 at 03:36
  • The exact problem is I don't know how to make the list view item physically start moving from right to left as the user is swiping. – b85411 May 31 '16 at 03:38
  • https://github.com/baoyongzhang/SwipeMenuListView – Aditya Vyas-Lakhan May 31 '16 at 03:39
  • I am trying to build it myself though, without the use of libraries. I was hoping maybe someone would know "this is you offset the view", "this is how you make it appear half way off the screen", etc – b85411 May 31 '16 at 03:40
  • Possible duplicate of [Swipe ListView item From right to left show delete button](http://stackoverflow.com/questions/20797099/swipe-listview-item-from-right-to-left-show-delete-button) – Trung Nguyen May 31 '16 at 04:09

1 Answers1

1

If you know when the swipe is happening and how much the user has swiped you could call View.setTranslationX() depending on how much the user has swiped. Alternatively, you could forego the swipe detection logic altogether and use a ViewPager with only 2 pages. You would then override PagerAdapter.getPageWidth(...) in your PagerAdapter so that the delete button only takes up a limited amount of space.

Andrew Orobator
  • 7,978
  • 3
  • 36
  • 36
  • Thanks, this is definitely the sort of info I was after. I tried `setTranslationX(-200)` as a trial and that worked. As a follow up to that, how can I make it so the second button appears? Because all that currently does is offset the position of the existing button? Thank you again – b85411 May 31 '16 at 04:06
  • To make the second button "appear" you could put it in your layout to the side of the list item, with the list item width being match parent. Then when you shift the entire layout to the left, the delete button will come into view. Honestly, I think the ViewPager solution would be much easier code wise, unless you're bent on implementing the touch logic yourself. – Andrew Orobator May 31 '16 at 04:16
  • Thanks for your response. With your ViewPager idea, wouldn't that affect the whole activity? Or is it possible to have the ListView be a list of ViewPager's? Such that whenever an item is swiped I'd see the delete button only for that item? – b85411 May 31 '16 at 04:53
  • You'd have a listview of viewpagers – Andrew Orobator May 31 '16 at 04:58