Android 导航抽屉中每个片段的不同工具栏

Android 导航抽屉中每个片段的不同工具栏,android,fragment,toolbar,android-collapsingtoolbarlayout,navigation-drawer,Android,Fragment,Toolbar,Android Collapsingtoolbarlayout,Navigation Drawer,我有一个主活动,它有一个导航抽屉。此NavigationDrawer最初与工具栏同步。我想在整个应用程序中使用唯一的NavigationDrawer。现在的问题是,每个片段都有不同的工具栏或折叠工具栏。 我已经读过同样的问题,但没有从这里找到答案: 在这篇文章中,一个人给出解决方案,但解决方案会导致内存泄漏 现在我想问,我如何才能做到这一点。我尝试了很多资源,但还没有找到解决方案 有些人正在使用BaseActivity方法,但我不想尝试这种方法,因为这种方法每次都需要对整个Navigation

我有一个主活动,它有一个导航抽屉。此NavigationDrawer最初与工具栏同步。我想在整个应用程序中使用唯一的NavigationDrawer。现在的问题是,每个片段都有不同的工具栏或折叠工具栏。

我已经读过同样的问题,但没有从这里找到答案:

在这篇文章中,一个人给出解决方案,但解决方案会导致内存泄漏

现在我想问,我如何才能做到这一点。我尝试了很多资源,但还没有找到解决方案

有些人正在使用BaseActivity方法,但我不想尝试这种方法,因为这种方法每次都需要对整个NavigationDrawer进行膨胀。


您好,只需在main_activivty.xml的工具栏下方放置一个框架布局即可。 现在,每次需要从一个片段切换到另一个片段时,只需替换为framelayout id

遵循以下步骤:

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.futuretech.animewallhd.Main2Activity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>



</android.support.design.widget.CoordinatorLayout>
<?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:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            layout="@layout/app_bar_main2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main2"
        app:menu="@menu/activity_main2_drawer"
        />

</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>



</RelativeLayout>
Fragment.java

public class DetailFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout,
                container, false);
        return view;
    }
}

将导航抽屉布局放在主活动中,并在导航抽屉的容器部分采用框架布局。现在,在每个导航上单击将新片段添加到抽屉容器。在活动中使用公共方法打开抽屉。然后为每个片段添加工具栏,并在工具栏上的导航图标调用活动的“打开抽屉”方法。

在这种情况下,您需要在主活动中使用导航抽屉。 现在为抽屉中的每个选项创建单独的片段

在android studio项目结构的res文件夹中没有,您需要创建名为“菜单”的新文件夹

在这个菜单文件夹中,您需要为每个片段创建xml文件。 将您的项目放入这些菜单文件夹中


在这里,我找到了一个关于


菜单文件夹中文件的示例代码如下

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_update"
        android:orderInCategory="100"
        android:title="Update Profile"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_logout"
        android:orderInCategory="100"
        android:title="Logout"
        app:showAsAction="never" />
</menu>
public class NotificationFragment extends Fragment {

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_notification, container, false);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.notification_menu, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_update) {
            Toast.makeText(getActivity(), "Update clicked!", Toast.LENGTH_SHORT).show();
            return true;

        }else if(id == R.id.action_logout){
            Toast.makeText(getActivity(), "Logout clicked!", Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

但我的工具栏也随着每个片段而变化。有些片段有简单的工具栏,有些片段有折叠工具栏。这些工具栏应该与导航抽屉链接。从每个片段中删除工具栏(MainActivity除外),但问题仍然是一样的。每个片段工具栏都应该与导航抽屉同步。每次我同步新工具栏时,先前同步的工具栏都会导致内存泄漏。如果你也有折叠工具栏,那么你必须使用两个工具栏,一个用于所有片段,一个用于折叠。当你从片段布局中删除工具栏时,也从fragment.java中删除工具栏,你不需要每次启动时都初始化它们在MainActivityproblem中已经足够了,当我将片段工具栏与导航抽屉同步时。然后先前同步的工具栏导致内存泄漏。制作一个通用工具栏并将其包含在所有片段中。。。。当你想改变fragmnet时,只需替换它们,每个片段都有不同的工具栏。。。我在问题中提到了?没问题。。为此,您只需调用方法open drawer。。代码的ret将是独立的。不会有内存泄漏,因为没有人依赖任何人。是否需要将片段工具栏与MainActivity的导航抽屉同步???
public class NotificationFragment extends Fragment {

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_notification, container, false);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.notification_menu, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_update) {
            Toast.makeText(getActivity(), "Update clicked!", Toast.LENGTH_SHORT).show();
            return true;

        }else if(id == R.id.action_logout){
            Toast.makeText(getActivity(), "Logout clicked!", Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}