向上导航(操作栏的后退箭头)不适用于片段

时间:2015-11-16 13:01:42

标签: android android-layout android-fragments android-actionbar android-toolbar

我将抽屉布局作为我活动的基本布局,我正在替换此抽屉布局内的框架上的两个碎片。第一个片段未添加到片段的后栈中。我在我的活动中显示汉堡包图标(我还想在我的第一个片段中使用抽屉菜单)。在第二个片段中,我按<div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <nav class="navbar navbar-default" role="navigation"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button> <a class="navbar-brand" href="#">Brand</a> </div> <div class="hidden-xs"> <ul class="nav navbar-nav navbar-right"> <li><a href="#">Link</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown<strong class="caret"></strong></a> <ul class="dropdown-menu"> <li><a href="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li class="divider"></li> <li><a href="#">Separated link</a></li> </ul> </li> </ul> </div> <div class="visible-xs hidden-sm hidden-md hidden-lg"> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav navbar-right"> <li><a href="#">Link</a></li> <li><a href="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li class="divider"></li> <li><a href="#">Separated link</a></li> </ul> </div> </div> </nav> </div> </div> </div> 禁用了汉堡图标,并使用mActionBarDrawerToggle.setDrawerIndicatorEnabled(false)启用了后退按钮。

在onResume的第一个片段中,我通过mActionBarDrawerToggle.setDrawerIndicatorEnabled(true)启用汉堡图标,以便当用户从第二个片段按下按钮(硬件和动作栏的向上按钮)时,用户将返回第一个片段和汉堡图标将启用。一切都很好,只是我无法从第二个片段动作栏后退按钮返回。我无法点击它。 以下是我的代码: -

活动代码

actionBar.setDisplayHomeAsUpEnabled(true)

活动XML

if (Utility.isLargeScreen(this))
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    else
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    mHiddenGemsApplication = (HiddenGemsApplication) getApplication();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    initViews();

    setSupportActionBar(mToolbar);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    mTextViewActionBarTitle.setText(getString(R.string.app_name));

    mActionBarDrawerToggle = new ActionBarDrawerToggle(HomeActivity.this, mDrawerLayout, mToolbar, R.string.open_drawer, R.string.close_drawer) {

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };

    mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);

    mActionBarDrawerToggle.syncState();

    mFragmentManager = getSupportFragmentManager();

    replaceFragment(new CategoryFragment(), getString(R.string.app_name), CategoryFragment.TAG);

 @Override
public void onBackPressed() {

    if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
        mDrawerLayout.closeDrawers();
        return;
    }
    super.onBackPressed();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
            if (mFragmentManager.getBackStackEntryCount() > 0) {
                mFragmentManager.popBackStack();
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

 public void replaceFragment(Fragment fragment, String actionBarTitle, String tag) {

    if (mFragmentManager == null)
        return;

    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.content_frame, fragment, tag);
    if (!tag.equals(CategoryFragment.TAG))
        fragmentTransaction.addToBackStack(tag);
    fragmentTransaction.commit();

    setActionBarTitle(actionBarTitle);
}

public void setActionBarTitle(String actionBarTitle) {
    if (!TextUtils.isEmpty(actionBarTitle))
        mTextViewActionBarTitle.setText(actionBarTitle);
}

public void setDrawerIndicatorEnabled(boolean value) {
    if (mActionBarDrawerToggle != null) {
        mActionBarDrawerToggle.setDrawerIndicatorEnabled(value);
    }
}

First Fragment: -

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="nirvaniclabs.com.hiddengems.activities.HomeActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include
        android:id="@+id/toolbarlayout"
        layout="@layout/toolbar_layout" />

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbarlayout" />
</RelativeLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/navigation_items" />

</android.support.v4.widget.DrawerLayout>

第二片段

 private Button mButtonTemp;
private AppCompatActivity mActivity;
public static String TAG = "CategoryFragment";

public CategoryFragment() {
    // Required empty public constructor
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    if (context instanceof Activity)
        mActivity = (AppCompatActivity) context;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View viewGroup = inflater.inflate(R.layout.fragment_category, container, false);
    initViews(viewGroup);

    mButtonTemp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ((HomeActivity) mActivity).replaceFragment(new TripListFragment(), "Trip Fragment", TripListFragment.TAG);
        }
    });

    return viewGroup;
}

private void initViews(View viewGroup) {
    mButtonTemp = (Button) viewGroup.findViewById(R.id.btn_temp);
}

@Override
public void onResume() {
    super.onResume();
    ((HomeActivity) mActivity).setDrawerIndicatorEnabled(true);
    ((HomeActivity) mActivity).setActionBarTitle(getString(R.string.app_name));
}

另外,当我在第二片段时,我可以滑动并看到抽屉菜单。我不希望这种行为,抽屉菜单只应在片段1中打开。

如果我的代码有任何问题,请告诉我。

1 个答案:

答案 0 :(得分:2)

经过多年努力解决同样的问题后,我最终设法通过此代码获取了Up按钮。 您必须在onCreate()或onCreateView()

中设置setHasOptionsMenu
setHasOptionsMenu(true);

然后,在onOptionsItemSelected()中添加此检查以将up / home按钮按到您的开关()[或if ...]语句:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    x.L();

    switch (item.getItemId()) {
        case android.R.id.home :
            getActivity().onBackPressed();

            break;

        case R.id.mn_exit :
            exitFragment();

            break;

        default :
            break;
    }

    return super.onOptionsItemSelected(item);
}