Android的列表视图右/左挥笔像通话记录视图、通话记录、列表、Android

2023-09-12 10:52:29 作者:嘬深德爱蕞沉默

我有一个Android应用程序,其中有列表像这样的

I have an android application in which there is List View Like this one

现在我想执行相同的方式对列表项的左/右轻扫两个不同的活动如何本地通话记录的作品

Now I want to perform two different activities on right/Left swipe of the List Items in the same way How native call log works

列表中右键刷卡

我想这种刷卡的。任何人都可以知道如何做到这一点。基本上,我婉实施 SimpleOnGestureListener

I want this type of swipes. Can anyone know how to make it happen. Basically I wan to implement SimpleOnGestureListener.

我读通过的先生GAV Android的回答后 - 基本手势检测之后,我成功地实现刷卡左,右轻扫检测,但是唯一我没有收到关于刷卡已发生的列表项。

I have read a post answered by sir gav Android - basic gesture detection and after that I am successful in implementing left swipe and right swipe detection but the only thing I am not getting on which List Item the swipe has occurred.

更新 24 2013月

现在我可以用这个code检测刷卡动作 -

Now I am able to detect swipe action using this code --

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以这种方式

Now use it with your ListView in this fashion

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

在你的 setOnItemClickListener 则可以检测到这样的挥击事件

In your setOnItemClickListener you can detect the swipe events like these

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();

            }
        }
    }
});

不过还是我不能动画像这些轻扫

But still I am not able to animate the swipe like these

请帮我。

推荐答案

OnSwipeTouchListener.java:

OnSwipeTouchListener.java:

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

    public boolean onTouch(final View view, final MotionEvent motionEvent) {

        super.onTouch(view, motionEvent);
        return gestureDetector.onTouchEvent(motionEvent);

    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }
    public void onSwipeRight() {}
    public void onSwipeLeft() {}
    public void onSwipeTop() {}
    public void onSwipeBottom() {}
}

用法:

imageView.setOnTouchListener(new OnSwipeTouchListener() {

    public void onSwipeTop() {
        Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeRight() {
        Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeLeft() {
        Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeBottom() {
        Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
    }

});