Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View switches: how to block user interaction in-between?

Tags:

android

I use a view switcher to switch between views in a way that is similar to standard activities switching in a task. For example, the current view in the switcher might have a button that, when clicked, initiates the view switch: the current view, which is now obsolete, slides out, and is replaced by the new current view, which slides in.

The animated switch is fine only for one thing: I cannot find a correct way to tell the sliding out view to stop processing user events, like touch events. So what happens is, if done fast enough, and it doesn't have to be that fast, the user can click on the button that initiates the view switch more than once, which is bad. Once a click (or any user action) has initiated a view switch, I would like to invalidate and ignore all other user events on the sliding out view.

Is there a clean, standard way to do so? I've tried setEnabled(false) on the sliding out view, but it seems click listeners in child views are still handled afterward. The thing I want to avoid is looking for all event handlers and adding verification code that ensures that nothing is done if the view is actually sliding out.

like image 860
Simon Avatar asked Mar 21 '11 16:03

Simon


2 Answers

I had a similar problem and want to share my solution. I override the Activity.dispatchTouchEvent method as noted here. In doing so I intercept all touch events before they are dispatched to the other views.

public boolean mDisplayBlocked = false;

@Override
public boolean dispatchTouchEvent(MotionEvent pEvent) {
    if (!mDisplayBlocked) {
        return super.dispatchTouchEvent(pEvent);
    }
    return mDisplayBlocked;
}
like image 53
Mokus Avatar answered Oct 20 '22 18:10

Mokus


In your click listener, before you initiate a view switch, I believe you should be able to disable the listener.

I imagine other UI interactions will still be possible though. Perhaps you need a masterDisable method, which will disable all UI interactions on that View, which you can call before you make the view switch.

like image 20
nicholas.hauschild Avatar answered Oct 20 '22 17:10

nicholas.hauschild