2

Im having some issues with the Android onGestureListener, I have 3 Linear Layouts sitting next to each other horizontally in a Linear Layout, the Linear Layout position is set to the middle Layout onCreate, the middle layout also contains a listview inside of it, what I would like to happen is when I swipe left or right the layout moves, this seems to not pick up the gestures when I attempt to swipe left or right on the Linear Layout with the listview inside of it, but if I swipe right or left on the other views that dont have anything in them it picks up the gesture and the view animates accordingly, has anyone had this issue before or have an idea how to fix it? any help will go a long way thanks

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    return gestureDetector.onTouchEvent(event);
}

SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {

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

        float sensitvity = 50;
        if ((e1.getX() - e2.getX()) > sensitvity) {
            slideLeft();
        } else if ((e2.getX() - e1.getX()) > sensitvity) {
            slideRight();
        }

        return true;
    }

};

private void slideRight() {

    if (swipeDirection > -1) {
        if (swipeDirection == 0) {
            layoutContainer.animate().translationX(theDistance - 0)
            .setDuration(500);
        } else {
            //go to home
            layoutContainer.animate().translationX(0).setDuration(500);
        }

        swipeDirection--;
    }

}

private void slideLeft() {

    if (swipeDirection < 1) {
        if (swipeDirection == 0) {
            layoutContainer.animate().translationX(0 - theDistance)
            .setDuration(500);
        } else {
            layoutContainer.animate().translationX(0).setDuration(500);
        }

        swipeDirection++;
    }

}
Edmund Rojas
  • 6,376
  • 16
  • 61
  • 92

1 Answers1

0

The ListView itself has a gesture listener built in already (for scrolling the list), and possibly it also has an onItemClickListener on the individual items in the list. It can be that this interferes with your swipe behavior of the whole layout.

The solution explained here by Pinhassi has worked best for me so far: Android Swipe on List

probably you will need to expand on the onItemClickListener of your ListView and include the swipe detector mentioned above. Also, putting @Override in front of the line where you declare the onItemClick might be needed to override the listview listeners. That way, you will maintain clickable list items and you can perform swipes on them.

Community
  • 1
  • 1
sirio0816
  • 85
  • 1
  • 6