列表视图项向左滑动并向右滑动?

时间:2013-07-08 07:00:11

标签: android listview

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

如果是这样,如何应用android滑动向左或向右滚动打开不同的意图?我想要像默认情况下在Android中的联系人视图上的呼叫电话或消息一样。

由于

2 个答案:

答案 0 :(得分:54)

试试这个:

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;
    }
}

答案 1 :(得分:0)

我认为你可以OnTouchListener使用MotionEvents并按照ACTION_DOWN ACTION_MOVEUPCANCEL或{{1}}或{{1}}或{{1}}来抓住列表项目的填充,初始和当前动作{{1}}。