2016-03-06 24 views
0

我試過了。我沒有成功。我正在寫我的步驟。如果任何人都可以幫助我。如何在導航抽屜中使用片段?

  1. 創建使用Android Studio的新建項目,然後選擇 「抽屜式導航活動」
  2. 我把FrameLayout裏的主要活動中,如下

    <include 
        layout="@layout/app_bar_vshome" 
        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_vshome" 
        app:menu="@menu/activity_vshome_drawer" /> 
    
    <!-- Framelayout to display Fragments --> 
    <FrameLayout 
        android:id="@+id/frame_container" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" /> 
    

  3. 我做新類,如下使用v4.app.Fragment

    public class VSAllTopics extends Fragment{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
             Bundle savedInstanceState) { 
        // Inflate the layout for this fragment 
        return inflater.inflate(R.layout.fragment_all_topics, container, false); 
    } 
    } 
    
  4. 我做片段Mananger,如下,

    public class FragmentManager extends Fragment { 
    
    } 
    
  5. 調用**公共布爾onNavigationItemSelected(菜單項項)**

    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ft = fragmentManager.beginTransaction();

    if (id == R.id.nav_camera) { 
    
         ft.replace(R.id.frame_container, new VSAllTopics(), "VS_ALL").commit(); 
    

我研究了一點,但我沒能成功。它不是在呼喚。

如果我把它通過的意圖,它消除了導航;(

如何我可以使用側面菜單在適當的滿耳

THANKS

+0

你可以在這裏查看我的答案https://stackoverflow.com/a/47590216/5381331 –

回答

0

這裏是您正在尋找的鏈接:。

Fragment Navigation Drawer

+0

我相信他是按照這個教程,但犯了一個錯誤。只需回答一個鏈接,而不是從他的錯誤提供的信息是沒有幫助的。 – jonathanrz

+0

沒關係,歡迎:) – jonathanrz

0

你的導航視圖應該是在的FrameLayout佈局之後。

Android按照它在xml中顯示的順序繪製每個視圖。由於您的FrameLayout具有寬度和高度的match_parent,因此android會在您的NavigationView上方繪製它。

下面是與解決方案的代碼:

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

<!-- Framelayout to display Fragments --> 
<FrameLayout 
    android:id="@+id/frame_container" 
    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_vshome" 
    app:menu="@menu/activity_vshome_drawer" /> 
+0

我改變了它的位置,但它還沒有工作。它在單擊側面菜單時一次又一次地顯示相同的第一個屏幕。 –

0

建議使用navigationView抽屜layout.Easiest選項裏面是使用由Android工作室提供的導航活動enter image description here

然後ü可以編輯從

activity_main_drawer.xml項目

並添加您自己的項目及其各自的圖標。

,你也可以爲了你的活動使用片段在NavigationDrawer

使用這樣的

navigation_header_main.xml

編輯導航標題觀點:

public boolean onNavigationItemSelected(MenuItem item) { 
 
    // Handle navigation view item clicks here. 
 
    int id = item.getItemId(); 
 

 
    switch (id) { 
 
    case R.id.item1: 
 
     getSupportFragmentManager().beginTransaction().replace(R.id.fragment1, new item1Fragment()).commit(); 
 
     break; 
 

 
    case R.id.change_settings: 
 
     getSupportFragmentManager().beginTransaction().replace(R.id.fragment1, new item2Fragment()).commit(); 
 
     break; 
 

 
    } 
 

 
}