gpt4 book ai didi

fragment 中带有选项卡布局的Android抽屉导航

转载 作者:行者123 更新时间:2023-11-29 23:16:22 29 4
gpt4 key购买 nike

我是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>

基本上我的主要 Activity 中有一个抽屉导航和 3 个选项卡。这是一张照片:

enter image description here

一切如我所愿,我很高兴。


当我点击一个抽屉导航项目时,我开始了一个新的 Intent 来打开一个新的 Activity ,这很好,但是抽屉当然消失了(因为新 Activity 没有抽屉)。我想一直把抽屉放在屏幕上。

为了将抽屉保留在我的屏幕上,我认为我可以将上述 XML 的 TabLayout 作为 fragment 放置;这样抽屉就还在那里。但是我该怎么做呢?如何将 TabLayout 放在 fragment 中,以便用 TabLayout 或其他 fragment 替换此 fragment ?

我认为我可以放置一个 FrameLayout 而不是 TabLayout,但我不知道这是否是个好主意以及它如何工作。有帮助吗?

类似的问题还有this但他们没有帮助我,因为我仍然被困在这里


更多。如果您单击结果,ThisSeason Activity 将启动并且其中有一些 fragment (包括 ResultsFragments)。这行得通,但是没有抽屉导航了,我必须按后退按钮!

最佳答案

我会给你一个实现这个的总体思路。

  1. 为您的应用程序创建一个基本 Activity 。
  2. 然后为每个 Activity 制作 fragment ,包括带有选项卡的屏幕。因此,如果您在导航 View 中有 5 个项目,那么将创建总共 6 个 fragment 。
  3. 在主 Activity xml和java文件中添加导航 View 和相关功能。这基本上会将导航 View 添加到基本 Activity 中,并且由于您使用的是 fragment ,因此所有页面( fragment )都将具有导航 View 。

附加说明:您可以创建多个子类来处理特定功能,例如用于处理 actionMode、导航 View 和更新工具栏中的标题的子类。你明白了。

此外,所有 fragment 共有的所有功能都应仅添加到基础 Activity 中,而不是在各处重复相同的代码。

关于 fragment 中带有选项卡布局的Android抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55291283/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com