【问题标题】:Different toolbar for each fragment in navigation drawer导航抽屉中每个片段的不同工具栏
【发布时间】:2018-04-06 18:59:04
【问题描述】:

我有一个 MainActivity,它有一个 NavigationDrawer。此 NavigationDrawer 最初与工具栏同步。而且我想在整个应用程序中使用唯一的 NavigationDrawer。现在的问题是每个片段都有不同的工具栏或 CollapsingToolbar。

我已经阅读了同样的问题,但没有从这里找到答案:
Different toolbar for fragments and Navigation Drawer

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

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

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

【问题讨论】:

    标签: android fragment toolbar android-collapsingtoolbarlayout navigation-drawer


    【解决方案1】:

    您好,只需在 main_activivty.xml 的工具栏下方放置一个 frameLayout。 现在,每次您需要从一个片段切换到另一个片段时,只需用 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>
    

    ma​​in_activity.xml

    <?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>
    

    fragment_layout.xml

    <?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>
    

    MainActivity.java

    public class Main2Activity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {
    
        FrameLayout frameLayout;
        FragmentManager fragmentManager;
        Toolbar toolbar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            frameLayout = (FrameLayout) findViewById(R.id.frame);
            setSupportActionBar(toolbar);
            toolbar.setTitle("Nature");
            fragmentManager = getFragmentManager();
            replaceFragment(new TopFrag());
    
    
    
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            toggle.syncState();
    
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }
    
        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main2, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();
    
            if (id == R.id.top) {
                toolbar.setTitle("Nature");
                replaceFragment(new TopFrag());
    
            } else if (id == R.id.featured) {
                toolbar.setTitle("Love");
                replaceFragment(new FeatureFrag());
    
            } else if (id == R.id.most) {
                toolbar.setTitle("Featured");
                replaceFragment(new MostFrag());
    
            }
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    
        protected void replaceFragment(Fragment fragment) {
            fragmentManager.beginTransaction().replace(R.id.frame, fragment).commit();
        }
    
    
    }
    

    片段.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;
        }
    }
    

    【讨论】:

    • 但是我的工具栏也随着每个片段而变化。一些片段有简单的工具栏,一些片段有折叠工具栏。这些工具栏应该与导航抽屉链接
    • 从每个片段中删除工具栏(Mainactiviy 除外)
    • 但问题还是一样。每个片段工具栏都应该与导航抽屉同步。每次如果我同步新工具栏,之前同步的工具栏都会导致内存泄漏。
    • 如果你也有折叠工具栏,那么你必须使用两个工具栏一个用于所有片段,一个用于折叠
    • 当你从片段布局中删除工具栏时,也从 Fragment.java 中删除工具栏,你不需要在 MainActivity 中每次都初始化它们
    【解决方案2】:

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

    【讨论】:

    • 问题是当我将片段工具栏与导航抽屉同步时。那么之前同步的工具栏导致内存泄露。
    • 制作一个通用工具栏并将其包含在所有片段中......当你想改变fragmnet时,只需替换它们
    • 每个片段都有不同的工具栏...我在问题中提到过?
    • 没问题..为此,您只需调用方法打开抽屉..代码的 ret 将是独立的。不会有内存泄漏,因为没有人依赖任何人。
    • 是否需要将fragment工具栏与MainActivity的抽屉导航同步???
    【解决方案3】:

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

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

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


    这里我找到了android navigation drawer with different toolbar的教程


    菜单文件夹中文件的示例代码可以是这样的

    <?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);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多