根据可见片段处理活动工具栏可见性 [英] Handling activity toolbar visibility according to visible fragment

查看:69
本文介绍了根据可见片段处理活动工具栏可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android应用程序中,我有一个活动和很多片段.但是,我只想显示某些片段的工具栏,而对于其他片段,我希望该片段为全屏显示.最好的推荐方法是什么(根据可见片段显示和隐藏活动工具栏)?

In my android application I have one activity and many fragments. However, I only want to show the toolbar for some fragments, for the others I want the fragment to be fullscreen. What's the best and recommended way to do this (show and hide the activity toolbar according to the visible fragment)?

推荐答案

我更喜欢为此使用接口.

I preferred using interface for this.

public interface ActionbarHost {
    void showToolbar(boolean showToolbar);
}

使您的活动实现为ActionbarHost,并将showToolbar替换为.

make your activity implement ActionbarHost and override showToolbar as.

@Override
public void showToolbar(boolean showToolbar) {
    if (getSupportActionBar() != null) {
        if (showToolbar) {
            getSupportActionBar().show();
        } else {
            getSupportActionBar().hide();
        }
    }
}

现在片段中的内容从onAttach()

private ActionbarHost actionbarHost;
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof ActionbarHost) {
        actionbarHost = (ActionbarHost) context;
    }
}

现在,只要您想隐藏片段中的操作栏调用actionbarHost.showToolbar(false);.

now just if you want to hide action bar call actionbarHost.showToolbar(false); from fragment.

if (actionbarHost != null) {
            actionbarHost.showToolbar(false);
        }

我也建议在onDetach()

@Override
public void onDetach() {
    super.onDetach();
    if (actionbarHost != null) {
        actionbarHost.showToolbar(true);
    }
}

这篇关于根据可见片段处理活动工具栏可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆