如何为每个片段设置工具栏,如何处理抽屉切换和工具栏

时间:2017-12-11 19:25:29

标签: android android-fragments android-support-library android-toolbar

我在我的应用中使用单个活动和多个片段

现在因为在我的一些片段中我在工具栏中有自定义视图,所以我决定为每个片段分别设置工具栏。

如何为每个片段实现单独的工具栏,抽屉布局也在我的活动中 Home page category page

5 个答案:

答案 0 :(得分:4)

我遇到同样的问题,我会为每个片段添加自定义工具栏视图。

我的实用工具方法是:

public static View addRemoveViewFromToolbar(FragmentActivity fragmentActivity, int resourceId) {
    Toolbar toolbar = removeViewFromToolbar(fragmentActivity);
    if (resourceId == 0) {
        return null;
    } else {
        View view = LayoutInflater.from(fragmentActivity).inflate(resourceId, toolbar, false);
        toolbar.addView(view);
        return view;
    }
}


public static Toolbar removeViewFromToolbar(FragmentActivity fragmentActivity) {
    Toolbar toolbar = (Toolbar) fragmentActivity.findViewById(R.id.toolbar);
    if (toolbar.getChildCount() > 1) {
        for (int i = 1; i <= toolbar.getChildCount(); i++) {
            toolbar.removeViewAt(1);
        }
    }
    return toolbar;
}

在我的每个片段中

//Create your custom view based on requirement
    View view = Utility.addRemoveViewFromToolbar(getActivity(), R.layout.toolbar_search_view);
        if (view != null) {
            edtCategory1 = (EditText) view.findViewById(R.id.edtCategory1);
            edtCategory1.setOnClickListener(this);
        }

希望这个解释可以帮到你:)

答案 1 :(得分:0)

我不确定我是否正确理解了您对应用的描述,但我最近做了我认为您所描述的内容。 我的活动布局是一个DrawerLayout,其中包含一个CoordinatorLayout / AppBar布局,下面是一个工具栏和一个FrameLayout。 menu.xml包含我工具栏中所有片段所需的所有项目。在导航菜单中单击的项目将交换FrameLayout中的片段。我的onNavigationItemSelected()调用此方法来交换片段并处理backstack:

 public void switchView(int id, int optArg) {
   if (currentView != id) {
     currentView = id; //currentView keeps track of which fragment is loaded
     FragmentTransaction transaction = getFragmentManager().beginTransaction();
     //Fragment contentFragment is the current fragment in the FrameLayout
     switch (id) {
       case 0: //menu item 1
         contentFragment = new Nav_Item1_Fragment();
         transaction.replace(R.id.fragment_container, contentFragment, "");
         break;
       case 1: //menu item 2
         contentFragment = new Nav_Item2_Fragment();
         transaction.replace(R.id.fragment_container, contentFragment, "");
         break;
       case 2: //menu item 3
         contentFragment = new Nav_Item3_Fragment();
         transaction.replace(R.id.fragment_container, contentFragment, "");
       break;
     }
     // Replace whatever is in the fragment_container view with this fragment,
     // and add the transaction to the back stack
     // transaction.replace(R.id.fragment_container, contentFragment);
     transaction.addToBackStack(null);
     // Commit the transaction
     transaction.commit();
   }
 }

并在每个片段的onPrepareOptionsMenu()中我setVisible()hid /显示工具栏中与该片段相关的菜单项。每个项目在菜单项的onClick属性指向的活动中都有一个方法,该方法知道它来自哪个片段以及传递给它的视图。

抽屉是在活动的onCreate()中使用ActionBarDrawerToggle设置的,如下所示:

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();

活动xml:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    ...>

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

    <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"
        .../>

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

app_bar_main.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"
    ...>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="top"
            .../>

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

    <FrameLayout
        android:id="@+id/fragment_container"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </FrameLayout>

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

所以......一个Nav菜单,一个App / Tool Bar,多个片段

答案 2 :(得分:0)

为什么您特别想为每个片段设置一个单独的工具栏? 您可以轻松更改每个片段的工具栏视图。

在切换片段的功能中 -

public void selectDrawerItem(MenuItem item) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction;
        switch (item.getItemId()) {

                    case R.id.nav_home :
                        getSupportActionBar().setCustomView(R.layout.home_nav_bar);
                        fragmentClass = HomeFragment.class;
                        break;
                    case R.id.nav_settings :
                        getSupportActionBar().setCustomView(R.layout.settings_nav_bar);
                        fragmentClass = SettingsFragment.class;
                        break;

        }
        fragment = (Fragment) fragmentClass.newInstance();
        fragmentManager.popBackStack(null, 
        FragmentManager.POP_BACK_STACK_INCLUSIVE);

        fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
}

因此,您可以轻松使用相同的工具栏,并根据每个片段的要求对其进行自定义。

答案 3 :(得分:0)

可以通过非常简单的方式完成。

  1. 首先使用drawerlayout创建活动。

  2. 其次在要保留的活动中创建一个容器viewpager fragments

  3. 第三个在viewpager上设置一个将设置的监听器 相关toolbar基于展示的fragment

  4. 让我用相关的XML和代码来说明

    首先是主要活动的Drawerlayout 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">
    <include
        layout="@layout/app_bar_landing_page"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <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_landing_page"
        app:menu="@menu/activity_landing_page_drawer" />
    
    </android.support.v4.widget.DrawerLayout>
    

    请注意容器布局app_bar_landing_page。现在是这个

    的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="me.veganbuddy.veganbuddy.ui.LandingPage">
    
        <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"
                android:elevation="4dp"
                app:logo="@drawable/vegan_buddy_menu_icon"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/container_for_fragments"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.design.widget.CoordinatorLayout>
    

    注意viewpager将充当片段的容器。现在是viewpager上的OnPageChangeListener

    mViewPager = (ViewPager) findViewById(R.id.container_for_fragments);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
            }
    
            @Override
            public void onPageSelected(int position) {
                switch (position+1){
                    case 0:setSupportActionBar(aToolbar);
                        break;
                    case 1:setSupportActionBar(bToolbar);
                        break;
                    case 2:setSupportActionBar(cToolbar);;
                        break;
                    case 3:setSupportActionBar(dToolbar);
                        break;
                    default:
                        break;
                }
            }
    

    如果需要进一步澄清,请告诉我

答案 4 :(得分:0)

我建议最简单的方法是使用回调来进行活动。每当片段加载到活动中时,安排回调活动并加载活动中的相应工具栏。

在布局文件夹中有单独的工具栏xmls。使用include标记将工具栏放入您的活动中。一次只能看到一个工具栏。当片段回调进​​入您的活动时,使必要的回调可见。