列表视图项向左滑动并向右滑动? [英] List view item swipe left and swipe right?

查看:39
本文介绍了列表视图项向左滑动并向右滑动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列表视图项可以响应向左滑动和向右滑动吗?

Can a List view item respond to swipe left and swipe right?

如果是这样,如何应用 android 向左或向右滑动来打开不同的意图?默认情况下,我希望在 android 中的联系人视图中呼叫电话或消息.

If so, how to apply android swipe left or right scrolling to open different intent? I want same like Call phone or message on contact view in android by default.

谢谢

推荐答案

试试这个:

navigaList.setOnTouchListener(swipeDetector);
navigaList.setOnItemClickListener(listener);

OnItemClickListener listener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long arg3) {
        if(swipeDetector.swipeDetected()) {
            if(swipeDetector.getAction() == Action.RL) {

            } else {

            }
        }        
    };

SwipeDetector.java

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 // when no action was detected
    }

    private static final String logTag = "SwipeDetector";
    private static final int MIN_DISTANCE = 100;
    private float downX, downY, upX, upY;
    private Action mSwipeDetected = Action.None;

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

    public Action getAction() {
        return mSwipeDetected;
    }

    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) > MIN_DISTANCE) {
                // left or right
                if (deltaX < 0) {
                    Logger.show(Log.INFO,logTag, "Swipe Left to Right");
                    mSwipeDetected = Action.LR;
                    return true;
                }
                if (deltaX > 0) {
                    Logger.show(Log.INFO,logTag, "Swipe Right to Left");
                    mSwipeDetected = Action.RL;
                    return true;
                }
            } else 

                // vertical swipe detection
                if (Math.abs(deltaY) > MIN_DISTANCE) {
                    // top or down
                    if (deltaY < 0) {
                        Logger.show(Log.INFO,logTag, "Swipe Top to Bottom");
                        mSwipeDetected = Action.TB;
                        return false;
                    }
                    if (deltaY > 0) {
                        Logger.show(Log.INFO,logTag, "Swipe Bottom to Top");
                        mSwipeDetected = Action.BT;
                        return false;
                    }
                } 
            return true;
        }
        }
        return false;
    }
}

这篇关于列表视图项向左滑动并向右滑动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆