【问题标题】:Fragments and right design碎片和正确的设计
【发布时间】:2012-01-31 14:49:22
【问题描述】:

抱歉,如果问题不清楚。在我的应用程序中,我在 acion 栏上有 3 个选项卡,此外,每个选项卡都有一个单独的 xml 布局,其中包含两个片段。前两个选项卡的布局相似,但第三个选项卡有一个单独的 xml 布局。在第一个选项卡中,我进行休息调用并获取要显示的数据。左侧片段显示文件夹列表,右侧片段显示文件列表。 Tab2 相同,但它显示来自 SD 卡的数据。它基本上是一个文件管理器。

第三个标签是搜索页面,与前两个片段完全不同。

下面是我的问题。

  1. 我想知道是否可以为每个选项卡使用不同的活动?这是正确的设计吗?
  2. 由于我使用主要活动来创建片段所需的布局,所以我现在遇到第三个选项卡的问题,原因是 xml 布局和片段类(代码)已完全更改为第三个选项卡。这是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 文件中移动了片段并在主活动代码中执行此操作。我很担心,因为我不确定这是否是正确的设计。

另外,下面是代码sn-p。

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;

每当用户单击选项卡时,我都会为该 perticulat 选项卡添加 xml。 下一个问题,我早先说过我使用 rest 调用从服务器获取数据来更新 tab1 片段,因为我使用 tab1 和 tab2 的列表片段,所以我只做 setListAdapter,但是我想了解我是否可以实现一个 backstack,或者我需要一个新的片段事务,每次单击列表项,然后将片段添加到 backstack。

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

谢谢, 哈沙

【问题讨论】:

  • 您需要更加具体。缩小您的问题范围。
  • 如果您需要帮助,请发布有关您提到的“崩溃问题”的 logcat 输出。
  • 对不起,如果问题不清楚。

标签: android android-activity android-fragments


【解决方案1】:

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

我们解决了一个主要活动,四个不同的选项卡,一个显示所有片段的主要 xml (8)。

谢谢, 哈沙

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2011-06-02
    • 1970-01-01
    • 2015-04-25
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多