如何滑动 Android 中左侧的列表视图项目?

How to swipe list view item left in Android?

我有一个列表视图,我想制作它以便我可以滑动每个项目以显示删除按钮:

我已经弄清楚如何识别滑动事件,我正在使用此代码(listItem 属于 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;
        }
    });

当我滑动时,我可以在日志中看到正在识别滑动事件。

但是,我不确定如何让列表项开始向左消失?

如有任何帮助,我们将不胜感激。

如果您知道滑动发生的时间以及用户滑动了多少,您可以在 PagerAdapter 中调用 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(...),这样删除按钮只占用有限的 space.