gpt4 book ai didi

android - fragment 和正确的设计

转载 作者:行者123 更新时间:2023-11-29 02:10:03 27 4
gpt4 key购买 nike

如果问题不清楚,请见谅。在我的应用程序中,我在 acion 栏上有 3 个选项卡,而且每个选项卡都有一个单独的 xml 布局和两个 fragment 。对于前两个选项卡,布局相似,但第三个选项卡具有单独的 xml 布局。在第一个选项卡中,我进行休息调用并获取要显示的数据。左侧 fragment 显示文件夹列表,右侧 fragment 显示文件列表。 Tab2 相同,但它显示来自 SD 卡的数据。它基本上是一个文件管理器。

第三个选项卡是搜索页面,它与前两个 fragment 完全不同。

下面是我的问题。

  1. 我想知道是否可以为每个选项卡使用不同的 Activity ?这是正确的设计吗?
  2. 由于我正在使用主要 Activity 来创建 fragment 所需的布局,所以现在第三个选项卡出现问题,原因是 xml 布局和 fragment 类(代码)已针对第三个选项卡完全更改。这是 xml 文件的内容。

前两个标签的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frags1">
<fragment class="TitlesFragment"

android:id="@+id/browse_title"
android:visibility="gone"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_height="match_parent" android:layout_width="320dip"/>

<fragment class="ContentFragment"

android:id="@+id/browse_content"
android:visibility="gone"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_height="match_parent" android:layout_width="match_parent">
</fragment>
</LinearLayout>

Layout for the third tab:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settings"
android:background="@drawable/window"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true">

<fragment class="SearchFragment"
android:id="@+id/search_title"
android:visibility="gone"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="700dp"
android:paddingRight="5dip"/>

<fragment class="SearchContentFragment"
android:id="@+id/searchbrowse_content"
android:visibility="gone"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

因此,当我从第一个选项卡单击到 tab3(搜索)时,UI 会正确重新生成,但我无法从 tab3 返回到 tab1。为了解决这个问题,我从 xml 文件中移动了 fragment ,并在主要 Activity 代码中进行了处理。我很担心,因为我不确定这是否是正确的设计。

此外,下面是代码 fragment 。

switch(nTabSelected)
{
case ConnectedConstants.BROWSE:
if (getFragmentManager().findFragmentById(R.id.browse_title) == null)
{
setContentView(CreateMainLayout());
}
TitlesFragment titleFrag = (TitlesFragment) getFragmentManager()
.findFragmentById(R.id.browse_title);
titleFrag.resetCurPosition();
titleFrag.setCategory(nTabSelected);
if (bLoggedin) {
titleFrag.selectPosition(0);
}
break;

每次当用户点击选项卡时,我都会为那个细化选项卡扩充 xml。下一个问题,我早些时候说过我使用 rest 调用从服务器获取数据以更新 tab1 fragment ,因为我正在使用 tab1 和 tab2 的列表 fragment ,我只是做 setListAdapter,但是我试图了解我是否可以实现一个后台堆栈,或者我需要一个新的 fragment 事务,每次点击列表项,然后将 fragment 添加到后台堆栈。

如果我的问题听起来还不清楚,请告诉我。

谢谢,戒日

最佳答案

我们做错了,我们试图在单击每个选项卡时膨胀不同的 xml 文件,简单的解决方案是让单个 XML 包含所有 fragment 并在单击操作栏上的选项卡时隐藏/显示它们。

我们解决了一个主要 Activity 、四个不同的选项卡、一个显示所有 fragment (8) 的主要 xml。

谢谢,戒日

关于android - fragment 和正确的设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8022566/

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