Android可扩展列表,实现手势,设置onTouch Listener使可扩展列表消失

时间:2012-03-15 23:38:17

标签: android expandablelistview gesture swipe

下面的示例使我的可扩展列表在我设置OnTouch侦听器或onCLick侦听器时消失。

我有一个ActivityswipeDetector类:

public class ActivitySwipeDetector 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_UP: {
        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) {
                        Log.i(logTag, "Swipe Left to Right");
                        mSwipeDetected = Action.LR;
                        return false;
                    }
                    if (deltaX > 0) {
                        Log.i(logTag, "Swipe Right to Left");
                        mSwipeDetected = Action.RL;
                        return false;
                    }
                } else 

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

}

在我的主要课程中,我在onCreate方法的顶部添加以下内容 - 但前两行使Expandable列表消失:

final ActivitySwipeDetector swipeDetector = new ActivitySwipeDetector();
         list.setOnTouchListener(swipeDetector);

         list.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (swipeDetector.swipeDetected()){
                            // do the onSwipe action 
                        } else {
                            // do the onItemClick action
                        }
                    }
                                });
            list.setOnItemLongClickListener(new OnItemLongClickListener() {
                public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
                    if (swipeDetector.swipeDetected()){
                        // do the onSwipe action
                        return true;
                    } else {
                        // do the onItemLongClick action
                        return false;
                    }
                }
            });

“list”定义为list = getExpandableListView();

所以1)我做错了什么让可扩展列表消失

2)我应该使用onGroupClick监听器和onChildClick监听器吗?如果是这样,请告知我添加它们的地方

3)如果说“执行滑动操作”和“执行OnItemClick操作”,我会添加什么?

我将使用从左向右滑动来扩展列表中的项目

从右向左滑动返回,我有一个已经有代码的按钮,所以我应该用我假设的滑动替换它?

非常感谢任何帮助!

Z

1 个答案:

答案 0 :(得分:1)

发现错误

在设置list = getExpandableListView()之前,我在主类中添加了侦听器;

当我插入听众后,它工作正常!