8

我的应用程序使用Multi Pane layout来显示分配列表。每个Assignment可以放在一个AssignmentCategory。我想使用DrawerLayout来显示所有AssignmentCategories,以便用户可以在不同类别之间轻松切换。抽屉布局和多窗格布局

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

回答

7

结合这两个项目不应该太困难。在示例代码中,DrawerLayout示例替代了内容片段,但您不必执行相同的操作,只需更新相同的片段即可显示正确的数据。你可以这样实现这两个项目:

  • 从多窗格演示项目开始。
  • 更新多窗格演示的两项活动,以延伸ActionBarActivity(V7),你不需要延长FragmentActivity
  • 实现DrawerLayout(从抽屉工程示例代码)代码在启动列表中的活动(我假设你不想在细节活动中使用DrawerLayout,但是如果你需要的话,实现它应该不成问题)。
  • 开始列表活动的布局将是这样的(不要忘记,你需要实现在activity_item_twopane.xmlDrawerLayout变化以及!):

    <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()]); 
    } 
    
  • 更新方法将是这样的:

我做了一个样本项目来说明上述变化,你可以find here

/** 
* 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; 
} 

- 各种其他小的变化。