也可以使用ActionBarDrawerToggle进行Backstack

时间:2015-12-07 08:34:51

标签: android

我正在使用DrawerLayout来设置侧边菜单,我使用默认设置抽屉片段:

public void setUp(int fragmentId, DrawerLayout drawerLayout, Toolbar toolBar)

我还在MainActivity中有一个FrameLayout(它承载抽屉片段)。 我将片段动态地交换到这个容器中。 我想要做的是更改工具栏标题,并在容器中有任何片段时显示后退按钮。否则显示通常的汉堡图标打开抽屉。

我是否可以使用相同的ActionBarDrawerToggle作为后台堆栈。或者有没有办法在每个片段中使用一个工具栏,它有一个后退按钮和标题?

我尝试在每个片段中使用工具栏,但它看起来不像默认操作栏(当然是因为片段意味着有操作栏)。

我看到Gmail应用程序通过ActionBarDrawerToggle本身执行此操作(当新片段出现时,即打开邮件时将其更改为后退按钮)。有人可以指出如何做到这一点。

CODE:

活动布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

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

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

    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="com.xxx.yyy.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        tools:layout="@layout/fragment_navigation_drawer" />

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

活动onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setSupportActionBar(toolBar);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.fragment_navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout), toolBar);
    ((DrawerLayout) findViewById(R.id.drawer_layout)).closeDrawer(GravityCompat.START);
}

Fragment的setUp方法:

/**
 * Users of this fragment must call this method to set up the navigation drawer interactions.
 *
 * @param fragmentId   The android:id of this fragment in its activity's layout.
 * @param drawerLayout The DrawerLayout containing this fragment's UI.
 * @param toolBar
 */
public void setUp(int fragmentId, DrawerLayout drawerLayout, Toolbar toolBar) {
    mFragmentContainerView = getActivity().findViewById(fragmentId);
    mDrawerLayout = drawerLayout;

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // set up the drawer's list view with items and click listener

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the navigation drawer and the action bar app icon.
    mDrawerToggle = new ActionBarDrawerToggle(
            getActivity(),
            mDrawerLayout,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close
    ) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (!isAdded()) {
                return;
            }

            getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if (!isAdded()) {
                return;
            }
            getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
        }
    };

    // Defer code dependent on restoration of previous instance state.
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });

    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

1 个答案:

答案 0 :(得分:4)

这就是我通常如上所述的方式

第1步
让工具栏成为您主要活动的一部分,如下所示:[您已经完成了!]

<DrawerLayout>
   <LinearLayout>
      <Toolbar/>
      <FrameLayout/>
   </LinearLayout>
<NavigationView>

第2步
在主要活动中填充片段时,在ActionBarDrawerToggle上禁用drawerindicator,如下所示:

toggle.setDrawerIndicatorEnabled(false);   //disables hamburger icon
getSupportFragmentManager().beginTransaction()
                           .replace(R.id.content,frag)
                           .addToBackStack(null)
                           .commit();

第3步
在片段生命周期方法中更改片段的标题,如下所示:

  @Override
    public void onActivityCreated(Bundle savedInstance){
     super.onActivityCreated(savedInstance);
    ((ActionBarActivity or AppCompatActivity)getActivity).getSupportActionBar().setTitle("Name of your fragment"); 
   }

第4步: 您可以在片段类中处理后退按钮,如下所示:

@Override
public void onOptionsItemSelected(MenuItem item){
 switch(item.getItemId()){
       case android.R.id.home://this will handle back navigation click
           getActivity().onBackPressed();
           break;
       }
  return super.onOptionsItemSelected(item);
  }

希望它有所帮助!如有任何疑问,请在下方发表评论..