Action bar is hidden and that shown instantly after that

自作多情 提交于 2019-12-12 22:13:46

问题


I'm trying to toggle show/hide action bar on user click on activity, so I've implemented this functionality like this in activity:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    Log.d("ACTION BAR", "triggered");

    super.dispatchTouchEvent(ev);

    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();

    if (actionBar.isShowing()) {
        actionBar.hide();
    } else {
        actionBar.show();
    }

    return true;
}

However, the problem is that when clicked on activity, the action bar gets hidden but then immediately is shown again. I've added logging and it seems that this method is triggered twice, why so?


回答1:


I think dispatchTouchEvent might be called two time on touch down and up action so take one boolean flag and check this flag value before showing action bar :

private boolean isManuallyHideShownActionBar;

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    super.dispatchTouchEvent(ev);

    ActionBar actionBar = getSupportActionBar();

    if(!isManuallyHideShownActionBar){
        if (actionBar.isShowing()) {
            actionBar.hide();
        } else {
            actionBar.show();
        }
        isManuallyHideShownActionBar = true;
    }else{
        isManuallyHideShownActionBar = false;
    }

    return true;
}


来源:https://stackoverflow.com/questions/30117177/action-bar-is-hidden-and-that-shown-instantly-after-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!