【问题标题】:Fragments Replacing Fragments and Navigating Fragments usefully片段替换片段和有用地导航片段
【发布时间】:2011-07-25 22:51:32
【问题描述】:

所以我有这个很好用的 ClientListView,它显示客户,我可以点击一个客户并在右侧获取他们的详细信息(在我的第二个片段中)。此处由此布局定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment class="com.viciousbytes.studiotab.subactivities.ClientListView"
        android:id="@+id/client_list" android:layout_weight="1"
        android:layout_width="0px" android:layout_height="match_parent" />

<FrameLayout android:id="@+id/client_details" android:layout_weight="1"
        android:layout_width="0px" android:layout_height="match_parent"
        android:background="?android:attr/detailsElementBackground" />
</LinearLayout>

这很好用,但后来我意识到我的另一个活动(即显示片段的片段活动)占据了整个屏幕,最好将其分成两部分。

所以我开始更改一些最初显示此片段活动的代码

void showSessionEdit()
{
 ...

 Intent intent = new Intent(getActivity(), EditSessionActivity.class);
            //  Send the recipe index to the new activity
            intent.putExtra(EditSessionActivity.THE_SELECTED_CLIENT, (int)mClient.getID());
            intent.putExtra(EditSessionActivity.SELECTED_SESSION, sessionId);
            startActivityForResult(intent, 1899); 
 ....
}

这很好用,打开了我的会话编辑器,我点击返回返回我的客户和详细信息。虽然我意识到我希望我的会话编辑器更像我的客户列表/详细信息,它们都在同一个屏幕上。通过大量的错误试验,我终于用这个替换了上面的内容:

void showSessionEdit()
{
 ...
    SessionEdit details = (SessionEdit) getFragmentManager().findFragmentById(R.id.session_edit);               
                // Make new fragment instance to show the recipe
                details = SessionEdit.newInstance(mContext, mIsTablet, (int)mClient.getID(), sessionId);
                // Replace the old fragment with the new one
                FragmentTransaction ft = getFragmentManager().beginTransaction();                   
                ft.replace(R.id.client_list, details);
                ft.addToBackStack("client_list");
                // Use a fade animation. This makes it clear that this is not a new "layer"
                // above the current, but a replacement
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);                            

      ft.commit();
              ...
              //now add another fragment to the right, just to test i dont have my invoice fragment done yet so I just added session again to see if it would display it does.

SessionEdit details2 = (SessionEdit) getFragmentManager().findFragmentById(R.id.session_edit);              

                // Make new fragment instance to show the recipe
                details2 = SessionEdit.newInstance(mContext, mIsTablet, (int)mClient.getID(), sessionId);
                // Replace the old fragment with the new one
                FragmentTransaction ft2 = getFragmentManager().beginTransaction();
                ft2.replace(R.id.client_details, details2);
                ft.addToBackStack("client_details");
                // Use a fade animation. This makes it clear that this is not a new "layer"
                // above the current, but a replacement
                ft2.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);                           

      ft2.commit();
 ...
}

这很好用,虽然我意识到我替换的不是 "div" 可以说是布局而是片段本身,所以我对 findFragmentById 的引用不再是我的 client_details 类型或 client_list 类型但现在是SessionEdit 类型。所以经过更多的尝试和错误后,我学会了将标签添加到addToBackStack(tag),现在我可以找到fragmentsByTag,这很好用,并且后退按钮排序工作:单击返回将再次替换左侧的client_list,因此我可以单击客户端并获取他们在右边的细节,问题是,如果我的 client_list 再次回来,右边的细节仍然会显示我的会话片段。另一个问题是我的客户列表是ListFragment,所以当我进行替换时,我仍然可以看到新片段下方的列表,就好像它是赛璐珞之类的。很奇怪。

因此,我在用新片段替换原始片段方面取得了一些进展,但使用后退按钮导航不再像我刚刚执行两个片段并在每个片段上添加新活动时那样“开箱即用”其他。那么如何在多个片段中导航呢?理想情况下,我将有一个 SessionInvoiceActivity 将两个片段(左侧的客户列表,右侧的客户详细信息)同时替换为左侧的会话,右侧的发票。当点击后退按钮时,我会返回我的客户列表和客户详细信息?但这我不知道该怎么做,仍然传递我需要的信息。我也不清楚为什么当我更换片段时,下面可以看到原始片段?是xml布局定义问题吗?

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    我不确定你现在是否明白这一点,看来我落后了一个夏天。

    我发现布局中指定的片段(在 XML 中)和使用 FragmentManager 添加的片段很难混合。

    我遇到了类似的问题,我在 onCreate() 中添加了我的“主”片段,然后当通过大小或用户选择认为有必要时,将第二个片段“详细信息”片段放在“主”片段所在的位置或如果处于横向模式,则添加到第二个布局。当用户点击返回键时,它将显示一个没有“主”片段的空白屏幕。我想我可能已经解决了这个问题,方法是在 Activity 的 onStart() 中进行原始调用以显示我的“主”片段。然而,这似乎还有一个问题。从 onStart() 添加“主”片段时,请勿将事务添加到 BackStack。这样,将调用 onResume() 或您之前的 Activity 中的任何内容,因此不会让 Activity 出现空白屏幕。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多