【问题标题】:DrawerLayout and Multi Pane LayoutDrawerLayout 和多窗格布局
【发布时间】:2013-10-27 00:58:04
【问题描述】:

我的应用程序使用Multi Pane layout 来显示分配列表。每个Assignment 可以放在一个AssignmentCategory 中。我想使用DrawerLayout 来显示所有的AssignmentCategories,这样用户就可以在不同的类别之间轻松切换。

我没有设法创建这样的布局。在官方的DrawerLayout tutorial 中,DrawerLayoutActivity 会在用户单击项目时替换Fragment(在我的例子中是AssignmentCategory)。我面临的问题是多窗格布局需要FragmentActivity。我不知道如何创建一个包含多窗格布局的Fragment。有人设法做到这一点吗?

【问题讨论】:

    标签: android android-layout android-fragments master-detail navigation-drawer


    【解决方案1】:

    将这两个项目结合起来应该不会太难。在示例代码中,DrawerLayout 示例确实替换了内容片段,但您不必这样做,您可以简单地更新相同的片段以显示正确的数据。你可以这样实现这两个项目:

    • 从多窗格演示项目开始。
    • 更新多窗格演示的两个活动扩展ActionBarActivity(v7),你不需要扩展FragmentActivity
    • 在开始列表活动中实现DrawerLayout(抽屉项目的示例代码)代码(我假设您不希望在详细信息活动中使用DrawerLayout,但实现它不应该是如果你想要的话有问题)。
    • 开始列表活动的布局将是这样的(不要忘记您还需要在activity_item_twopane.xml中实现DrawerLayout更改!):

      <DrawerLayout>
           <fragment android:id="@+id/item_list" .../>
           <ListView /> <!-- the list in the DrawerLayout-->
      </DrawerLayout>
      
    • 更改实现DrawerItemClickListener,因此当用户单击抽屉列表项时,您不会创建并添加新的列表片段,而是从布局中更新单个列表片段:

      AssignmentListFragment alf = (AssignmentListFragment) getSupportFragmentManager()
              .findFragmentById(R.id.item_list);
      if (alf != null && alf.isInLayout()
              && alf.getCurrentDisplayedCategory() != position) {
          alf.updateDataForCategory(position); // the update method
          setTitle(DummyContent.CATEGORIES[alf.getCurrentDisplayedCategory()]);
      }
      
    • 更新方法是这样的:

      /**
      * This method update the fragment's adapter to show the data for the new
      * category
      * 
          * @param category
          *            the index in the DummyContent.CATEGORIES array pointing to the
      *            new category
      */
      public void updateDataForCategory(int category) {
          mCurCategory = category;
          String categoryName = DummyContent.CATEGORIES[category];
          List<DummyContent.Assigment> data = new ArrayList<Assigment>(
              DummyContent.ITEM_MAP.get(categoryName));
          mAdapter.clear(); // clear the old dsata and add the new one!
          for (Assigment item : data) {
                  mAdapter.add(item);
          }
      }
      
      public int getCurrentDisplayedCategory() {
              return mCurCategory;
      }
      

      -其他各种小改动

    我制作了一个示例项目来说明上述更改,您可以find here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      相关资源
      最近更新 更多