【问题标题】:Android Nested Fragment ApproachAndroid 嵌套片段方法
【发布时间】:2013-05-19 16:03:00
【问题描述】:

我是使用android片段的新手。我有一个场景让我解释一下我得到了

  1. 扩展FragmentActivity的主要活动 .我的主要活动包括两个片段
  2. 扩展片段的选项列表
  3. 扩展片段的详细信息列表
  4. 扩展片段的详细说明

当我启动我的主要活动时。它将由 Fragment 1 中的选项列表和 Fragment 2 中的详细信息列表组成。然后我从 Details List 中选择一个项目。那么它的Description必须加载到主Activity的Fragment 2中

看这张图

我不知道如何实现这一点。我的意思是如何告诉详细列表片段在主要活动中加载详细描述片段。此外,当我按下后退按钮时,我必须返回初始阶段,即

编辑:

我所做的是在片段中创建一个接口(监听器)并在我的父活动上实现它。但是如果有 10 个不同的片段,我需要在我的父活动中实现所有接口。那么还有其他方法可以实现这一目标吗?

【问题讨论】:

    标签: android android-fragments android-fragmentactivity


    【解决方案1】:

    在开发 Fragment 时要始终记住的是,它需要一个 UI 才能显示。您需要在布局中放置片段的位置。有两种方法可以做到这一点:

    • 创建一个 Fragment 类并在您的布局中声明它们,如下所示。

      这里我们假设我们已经创建了一个 ArticleListFragment 和 ArticleReaderFragment 片段。

      <fragment
          android:id="@+id/list"
          android:name="com.example.news.ArticleListFragment"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1" />
      
      <fragment
          android:id="@+id/viewer"
          android:name="com.example.news.ArticleReaderFragment"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="2" />
      

      这样做的缺点是您无法在运行时更改它,这意味着当您的应用程序正在执行时,您无法用另一个片段替换片段。例如,如果您必须显示两个片段,则必须在布局中声明两个片段并隐藏其中一个。幸运的是还有另一种方法。

    • 在运行时以编程方式添加您的片段。在这种方法中,您必须首先声明一个布局,并确保添加一个容器(LinearLayout、RelativeLayout 等)来放置片段。例如:

      <ListView
          android:id="@id/options_list"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1" >
      </ListView>
      
      <LinearLayout
          android:id="@id/detail_layout"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="2"
          android:orientation="vertical" >
      </LinearLayout>
      

    在这里,我为您的选项定义了一个列表 options_list 和一个布局 detail_layout,您需要在其中放置详细信息。现在在运行时,当单击一个选项时,您会在 detail_layout 上显示详细信息片段,例如:

    ExampleFragment fragment = new ExampleFragment();
    getFragmentManager().beginTransaction().add(R.id.detail_layout, fragment).commit();
    

    用另一个片段替换那个片段:

    Fragment newFragment = new AnotherExampleFragment();
    getFragmentManager().beginTransaction().replace(R.id.detail_layout, newFragment).addToBackStack(null).commit();
    

    注意对 addToBackStack 的调用。这是必要的,以便当用户按下返回时,将显示上一个。

    我想你可以从这里弄清楚。 :)

    【讨论】:

    • 其实我看过你提到的那篇文章。我所做的是在我的主要活动中实现接口以加载子片段。我想知道有没有更好的方法来处理它。你需要我所做的源代码吗??
    【解决方案2】:

    在您的片段活动中,您可以使用 2 个容器视图(例如 2 个线性布局) - 用于左侧导航(选项列表)和右侧内容。 然后你需要一个公共功能,比如 public void switchFragmentInContainer(Fragment f, int position) 在其中您将新片段加载到容器中...

    在您的 OptionList 中,您可以像这样调用此函数 ((FragmentActivity) getActivity).switchFragmentInContainer(NEWFRAGMENT, FragmentActivity.RIGHT);.

    我希望这是您搜索的内容。

    【讨论】:

      【解决方案3】:

      根据http://developer.android.com/guide/components/fragments.html#Adding

      // Create new fragment and transaction
      Fragment newFragment = new ExampleFragment();
      FragmentTransaction transaction = getFragmentManager().beginTransaction();
      
      // Replace whatever is in the fragment_container view with this fragment,
      // and add the transaction to the back stack
      transaction.replace(R.id.fragment_container, newFragment);
      transaction.addToBackStack(null);
      
      // Commit the transaction
      transaction.commit();
      

      您可能还需要:

      public class MainActivity extends FragmentActivity {
          ...
          public void onArticleSelected(Uri articleuri) {
              //use data to add the new fragment
          }
          ...
      } 
      
      public static class FragmentA extends ListFragment {
          OnArticleSelectedListener mListener;
      
          // Container Activity must implement this interface
          public interface OnArticleSelectedListener {
              public void onArticleSelected(Uri articleUri);
          }
          ...
          @Override
          public void onAttach(Activity activity) {
              super.onAttach(activity);
              try {
                  mListener = (OnArticleSelectedListener) activity;
              } catch (ClassCastException e) {
                  throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
              }
          }
          ...
      
          @Override
          public void onListItemClick(ListView l, View v, int position, long id) {
              // Append the clicked item's row ID with the content provider Uri
              Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
              // Send the event and Uri to the host activity
              mListener.onArticleSelected(noteUri);
          }
          ...
      }
      

      【讨论】:

      • 我认为这是来自开发者博客的文章形式。我经历过那个。我所做的是在我的主要活动中实现接口以加载子片段。我想知道有没有更好的方法来处理它。你需要我所做的源代码吗??
      • 代码有帮助,因为我不确定你在问什么。我给你的是直接来自 developer.android.com 的官方文档。如果片段与此活动相关,这就是您加载它的方式。如果片段与此活动无关,则开始一个新活动。
      猜你喜欢
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      相关资源
      最近更新 更多