giozh
giozh

Reputation: 10078

handle a single touch inside ViewPager

i have a ViewPager layout for implement swiping tabs. Everiting works fine, but i would handle, in every Fragment that compose my ViewPager, a single touch event. i've try to do this inside my main activity:

@Override 
public boolean onTouchEvent(MotionEvent event) {
    int action=event.getAction();
    if((action == MotionEvent.ACTION_DOWN) && (action == MotionEvent.ACTION_UP)) {
    getActionBar().show();
    return true;
    }
return false;
}

but it seems not work. How can i do that?

Upvotes: 0

Views: 573

Answers (2)

Robert Rowntree
Robert Rowntree

Reputation: 6289

Locate the class for your Fragment type that is being paged by the ViewPager.

In that class locate the view --say an imageView of images you are paging--

On that View add the listener, including in the local event handler a callback interface to your Activity , to which the fragments are attached.

In the local handler for the 'touch' event, callback to the interface.

In the fragments onAttach, verify that the Activity being attached to implements the handler.

The activity to fragment interface explained here

Your main activity can then implement what you really want for the touch event in the remote part of the callback.

Upvotes: 0

FoamyGuy
FoamyGuy

Reputation: 46856

You have at least one problem. Your if statment is checking to see if action equals both up and down. Which will never evaluate to true, since it is not possible for the value to be both up and down at the exact same time.

Try like this:

if((action == MotionEvent.ACTION_DOWN)) {
    getActionBar().show();
    return true;
}

Upvotes: 3

Related Questions