首页 文章

如何在导航抽屉中使用Fragment?

提问于
浏览
0

我试过了 . 我不成功 . 我正在写我的步骤 . 如果有人可以帮助我 .

  • 使用Android Studio创建新项目并选择"Navigation Drawer Activity"

  • 我将FrameLayout放在主要活动中,如下所示

<?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_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" />

</android.support.v4.widget.DrawerLayout>
  • 我使用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);
    }
}
  • 我制作了Fragment Mananger,如下所示,
public class FragmentManager extends Fragment {

}
  • 调用** public boolean onNavigationItemSelected(MenuItem item)**
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();

我研究了一点,但我无法成功 . 它不是在呼唤 .

如果我通过Intent调用它,它会删除NAVIGATION;(

我如何在正确的方式中使用侧面菜单 .

谢谢 .

3 回答

  • 0

    这是您要查找的链接:

    Fragment Navigation Drawer

  • 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

    建议在Drawer布局中使用navigationView . 最简单的选择是使用android studio提供的导航Activity
    enter image description here

    然后你可以编辑项目

    activity_main_drawer.xml

    并添加您自己的项目和他们各自的图标 .

    您也可以编辑导航 Headers 视图

    navigation_header_main.xml,以便在Activity中的NavigationDrawer中使用Fragment

    使用它像这样:

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

相关问题