0

the example below makes my Expandable List dissapear as soon as I set either the OnTouch listener or onCLick listener.

I have a ActivityswipeDetector Class:

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

}

And within my main class I add the following at the top of the onCreate method- but the first two lines make the Expandable list dissapear:

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

the "list" is defined as list= getExpandableListView();

So 1) what am I doing wrong that makes the expandable list dissapear

2) Should I be using onGroupClick listener and onChildClick listeners? If so please advise n where I add them

3) Where it says "do the swipe action" and "do the OnItemClick action", what do I add?

I am to use left-to-right swipes to expand on an item in the list

right-to-left- swipes to go back, and I have a button that has code for that already so i should just replace that with the swipe I assume?

Any Help would be greatly appreciated!

Z

user1180937
  • 121
  • 1
  • 1
  • 9

1 Answers1

1

Found the Error

I added the listeners in the main class before I had set list= getExpandableListView();

when I inserted the listeners afterwards it worked fine!

user1180937
  • 121
  • 1
  • 1
  • 9