Tôi không chắc chắn nếu tôi hiểu mô tả của bạn trong những ứng dụng của bạn một cách chính xác, nhưng gần đây tôi đã làm những gì tôi nghĩ rằng bạn đã mô tả. Bố cục hoạt động của tôi là một DrawerLayout với bố cục CoordinatorLayout/AppBar được bao gồm với một thanh công cụ duy nhất và FrameLayout bên dưới. Menu.xml chứa tất cả các mục tôi cần trong thanh công cụ của tôi cho tất cả các mảnh. Các mục được nhấp trong menu điều hướng sẽ hoán đổi các đoạn trong FrameLayout. My onNavigationItemSelected() kêu gọi các phương pháp này để trao đổi trên các mảnh vỡ và xử lý các 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();
}
}
và trong onPrepareOptionsMenu() Tôi setVisible mỗi đoạn() giấu/hiện các mục trình đơn trong thanh công cụ liên quan đến đoạn đó. Mỗi mục có một phương thức trong hoạt động được trỏ đến bởi thuộc tính onClick của mục trình đơn mà biết phân đoạn đó là gì và các khung nhìn nào được truyền cho nó.
Các ngăn kéo đã được thiết lập trong onCreate() của hoạt động này với một ActionBarDrawerToggle như vậy:
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();
hoạt động 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>
Vì vậy, ... Một menu Nav, một App/Tool Bar, nhiều đoạn
Bạn đã tìm thấy giải pháp nào chưa? –
không tìm thấy bất kỳ giải pháp – alphanso