如何在导航抽屉中使用片段?
我试过了。我没有成功。我正在写我的步骤。如果任何人都可以帮助我。如何在导航抽屉中使用片段?
- 创建使用Android Studio的新建项目,然后选择 “抽屉式导航活动”
-
我把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" />
-
我做新类,如下使用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); } }
-
我做片段Mananger,如下,
public class FragmentManager extends Fragment { }
-
调用**公共布尔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
这里是您正在寻找的链接:。
我相信他是按照这个教程,但犯了一个错误。只需回答一个链接,而不是从他的错误提供的信息是没有帮助的。 – jonathanrz
没关系,欢迎:) – jonathanrz
你的导航视图应该是在的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" />
我改变了它的位置,但它还没有工作。它在单击侧面菜单时一次又一次地显示相同的第一个屏幕。 –
建议使用navigationView抽屉layout.Easiest选项里面是使用由Android工作室提供的导航活动
然后ü可以编辑从
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; } }
你可以在这里查看我的答案https://stackoverflow.com/a/47590216/5381331 –