简体   繁体   English

如何隐藏顶部片段中的“向上按钮”并将其显示在其他片段中?

[英]How to hide the “Up button” in the top Fragment and show it in other Fragments?

In a Bluetooth-related app (with minSdkVersion="18" ) I have a single MainActivity.java , displaying one of the following 3 UI Fragments: 在与蓝牙相关的应用程序( minSdkVersion="18" )中,我有一个MainActivity.java ,显示以下3个UI片段之一:

  • MainFragment.java (the top screen) MainFragment.java (顶部屏幕)
  • SettingsFragment.java (settings screen, entered through menu) SettingsFragment.java (设置屏幕,通过菜单输入)
  • ScanningFragment.java (lists nearby Bluetooth devices) ScanningFragment.java (列出附近的蓝牙设备)

截图

To display an "Up button" and handle the "Back button" I have the following code in place: 要显示“向上按钮”并处理“后退按钮”,我有以下代码:

public class MainActivity extends Activity 
                          implements BleWrapperUiCallbacks {

    // set in onResume() of each fragment
    private Fragment mActiveFragment = null;

´   @Override
    public void onBackPressed() {
        if (!getFragmentManager().popBackStackImmediate())
            super.onBackPressed();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_root);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        if (savedInstanceState == null) {
            Fragment fragment = new MainFragment();
            getFragmentManager().beginTransaction()
                .replace(R.id.root, fragment, "main")
                .commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                getFragmentManager().popBackStackImmediate();
                break;

            case R.id.action_settings:
                Fragment fragment = new SettingsFragment();
                getFragmentManager().beginTransaction()
                    .addToBackStack(null)
                    .replace(R.id.root, fragment, "settings")
                    .commit();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

This works well, but has a cosmetic problem, that the "Up button" is still displayed when the MainFragment.java is being displayed - as you can see on the left side of the above screenshot. 这很有效,但是有一个美观的问题,当显示MainFragment.java时仍然会显示“向上按钮” - 正如您在上面的屏幕截图左侧所示。

I have tried calling 我试过打电话

getActionBar().setHomeButtonEnabled(false);

when that fragment is being active, but that only disables the "Up button" - without really hiding it. 当该片段处于活动状态时,但只禁用“向上按钮” - 而不是真正隐藏它。

With help of Little Child (thanks!) here my solution using the FragmentManager.OnBackStackChangedListener : 在小孩子的帮助下(谢谢!),这里是我使用FragmentManager.OnBackStackChangedListener的解决方案:

public class MainActivity extends Activity 
                          implements OnBackStackChangedListener,
                                    BleWrapperUiCallbacks {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        getFragmentManager().addOnBackStackChangedListener(this);
    }

    @Override
    public void onBackStackChanged() {
        getActionBar().setDisplayHomeAsUpEnabled(
            getFragmentManager().getBackStackEntryCount() > 0);
    }

You are in luck because in API level 18 there is this: 你很幸运,因为在API级别18中有这样的:

getActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);  

for support library, it is: 对于支持库,它是:

getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);  

So now depending upon what fragment you are in, you can change the icon of "up" button. 因此,现在根据您所在的片段,您可以更改“向上”按钮的图标。

Also, try this: 另外,试试这个:

getActionBar().setDisplayHomeAsUpEnabled(false);

Set whether home should be displayed as an "up" affordance. 设置是否应将主页显示为“向上”可供性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM