安卓列表视图如何实现类似通话记录的左右滑动功能?

30

我有一个Android应用程序,在其中有一个类似于此列表视图

enter image description here

现在我想以同样的方式在列表项向右/向左滑动时执行两种不同的活动,就像本机通话记录一样

列表向右滑动

enter image description here

我想要这种类型的滑动。 有人知道如何实现吗?基本上,我想要实现 SimpleOnGestureListener

我阅读了由 Fling gesture detection on grid layout回答的帖子,之后我成功实现了左滑和右滑检测,但唯一的问题是我无法确定发生滑动的列表项。

更新 2013年5月24日

现在,我能够使用此代码检测滑动操作 -

SwipeDetector.java

/**
 * Class swipe detection to View
 */
public class SwipeDetector implements View.OnTouchListener {

    public static enum Action {
        LR, // Left to right
        RL, // Right to left
        TB, // Top to bottom
        BT, // Bottom to top
        None // Action not found
    }

    private static final int HORIZONTAL_MIN_DISTANCE = 30; // The minimum
    // distance for
    // horizontal swipe
    private static final int VERTICAL_MIN_DISTANCE = 80; // The minimum distance
    // for vertical
    // swipe
    private float downX, downY, upX, upY; // Coordinates
    private Action mSwipeDetected = Action.None; // Last action

    public boolean swipeDetected() {
        return mSwipeDetected != Action.None;
    }

    public Action getAction() {
        return mSwipeDetected;
    }

    /**
     * Swipe detection
     */@Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            {
                downX = event.getX();
                downY = event.getY();
                mSwipeDetected = Action.None;
                return false; // allow other events like Click to be processed
            }
        case MotionEvent.ACTION_MOVE:
            {
                upX = event.getX();
                upY = event.getY();

                float deltaX = downX - upX;
                float deltaY = downY - upY;

                // horizontal swipe detection
                if (Math.abs(deltaX) > HORIZONTAL_MIN_DISTANCE) {
                    // left or right
                    if (deltaX < 0) {

                        mSwipeDetected = Action.LR;
                        return true;
                    }
                    if (deltaX > 0) {

                        mSwipeDetected = Action.RL;
                        return true;
                    }
                } else

                // vertical swipe detection
                if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) {
                    // top or down
                    if (deltaY < 0) {

                        mSwipeDetected = Action.TB;
                        return false;
                    }
                    if (deltaY > 0) {

                        mSwipeDetected = Action.BT;
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    }

}

现在以这种方式将其与您的ListView一起使用

    // Set the touch listener
    final SwipeDetector swipeDetector = new SwipeDetector();
    lv.setOnTouchListener(swipeDetector);

setOnItemClickListener中,您可以像下面这样检测滑动事件

lv.setOnItemClickListener(new OnItemClickListener() {


    @Override
    public void onItemClick(AdapterView <? > parent, View view,
        int position, long id) {

        if (swipeDetector.swipeDetected()) {
            if (swipeDetector.getAction() == SwipeDetector.Action.LR) {

                Toast.makeText(getApplicationContext(),
                    "Left to right", Toast.LENGTH_SHORT).show();

            }
            if (swipeDetector.getAction() == SwipeDetector.Action.RL) {

                Toast.makeText(getApplicationContext(),
                    "Right to left", Toast.LENGTH_SHORT).show();

            }
        }
    }
});

但是我仍然无法像这样使滑动动画活泼起来:

在此输入图像描述


1
也许这会有所帮助:https://dev59.com/GGgt5IYBdhLWcg3wwwO- - Elior
@Nikhil Agrawal,您解决了这个问题吗? - Alok Agarwal
@Nikhil Agrwal:我也在寻找同样的解决方案,你有找到任何解决方法吗? - Jayesh
1个回答

1
您可以使用Fragments来满足此要求。使用FragmentPagerAdapter并覆盖getItem(int position)方法,该方法将返回Fragment。 将适配器设置为viewPager,并在setOnPageChangeListener中编写调用不同片段的逻辑即可。
Shrishail

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接