android - 带有标签布局的Android导航抽屉片段
问题描述
我是 android 新手,我将设计这种对我来说很复杂的布局。这是main_activity.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"
android:id="@+id/drawer_layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<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=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/home_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<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"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>
基本上,我的主要活动中有一个导航抽屉和 3 个选项卡。这是一张图片:
一切都按我的意愿进行,我很高兴。
当我点击一个导航抽屉项目时,我开始一个新的意图来打开一个新的活动,这很好,但抽屉当然消失了(因为新活动没有抽屉)。我想把抽屉一直放在屏幕上。
为了让抽屉保持在我的屏幕上,我认为我可以将上述 XML 的 TabLayout 作为片段放置;这样,抽屉仍然在那里。但是我该怎么做呢?如何将 TabLayout 放在片段中,以便可以用 TabLayout 或其他片段替换此片段?
我认为我可以放置一个 FrameLayout 而不是 TabLayout,但我不知道这是否是个好主意以及它将如何工作。有什么帮助吗?
有类似的问题,但他们没有帮助我,因为我仍然被困在这里
更多的。如果单击结果,则会启动 ThisSeason 活动,其中包含一些片段(包括 ResultsFragments)。那行得通,但是不再有导航抽屉了,我必须按后退按钮!
解决方案
我会给你一个实现这个的总体思路。
- 为您的应用程序创建一个基本活动。
- 然后为每个活动制作片段,包括具有标签的屏幕。因此,如果您在导航视图中有 5 个项目,则将创建总共 6 个片段。
- 在主活动 xml 和 java 文件中添加导航视图和相关功能。这基本上会将导航视图添加到基本活动中,并且由于您使用的是片段,因此所有页面(片段)都将具有导航视图。
附加说明:您可以创建多个子类来处理特定功能,例如用于处理 actionMode、导航视图和更新工具栏中标题的子类。你明白了。
此外,所有片段共有的所有功能都应该只添加到基本活动中,而不是在任何地方重复相同的代码。
推荐阅读
- python - 仅来自左表的数据的左外连接 -pandas
- java - 在java中使用axis2添加自定义肥皂头
- java - Swing中的多个依赖组合框
- python - 编辑时修改 QTableWidgetItem 上下文菜单
- javascript - (健全性检查)对于这个简单的用例,我需要一个时区库吗?
- scala - SBT 程序集从另一个 JAR 中删除类
- java - 签名的 Android 应用程序不会安装在 Galaxy S20 上
- python - 如何在python中将整数转换为64位大端整数
- python - spaCy没有在句子中提取所有ORG标签
- python - 我有一个字符串“hello\n1hello123\n2yahoo”。想用 \n[整数值] 分割它