Search code examples
javaandroidlistviewswipedetection

Switch Screen/Activity By Swiping Android Java


I have an application with different screens/activity's. At the moment it works fine, but I've a previous and next button. I'll remove this button, and use a swipe move to "say" next of previous. I found a lot on the internet, Something about Action detection, But it doesn't work with my listview. When I swipe on a textview it works fine, but when I put the same code in my code with the listview, it doesn't detect anything anymore.

Can anybody help me?

In my own code I use a: ListAdapter, onItemClick and a public View getView to put some images and textviews in my listview.

The onTouchEvent what does not work with my listview.

public boolean onTouchEvent(MotionEvent touchevent) {
    switch (touchevent.getAction()) {
        // when user first touches the screen we get x and y coordinate
        case MotionEvent.ACTION_DOWN: {
            x1 = touchevent.getX();
            y1 = touchevent.getY();
            break;
        }
        case MotionEvent.ACTION_UP: {
            x2 = touchevent.getX();
            y2 = touchevent.getY();

            // if left to right sweep event on screen
            if (x1 < x2) {
                Toast.makeText(this, "Left to Right Swap Performed", Toast.LENGTH_LONG).show();
            }

            // if right to left sweep event on screen
            if (x1 > x2) {
                Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
            }

            // if UP to Down sweep event on screen
            if (y1 < y2) {
                Toast.makeText(this, "UP to Down Swap Performed", Toast.LENGTH_LONG).show();
            }

            // if Down to UP sweep event on screen
            if (y1 > y2) {
                Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show();
            }
            break;
        }
    }
    return false;
}

Update

But everything I find is with different classes. In the examples, they use fragment1.java, fragment2.java, fragment3.java. But I have only my main_activity.java and these is running, and when I click my next button, it reloads the main activity, and shows other information And again and again. till my counter is on its max.


Solution

  • I think swipe should be from one fragment to another fragment not from activity to another avtivity..

    http://android-developers.blogspot.in/2011/08/horizontal-view-swiping-with-viewpager.html

    http://developer.android.com/training/implementing-navigation/lateral.html