抽屉式布局和多窗格布局

8
我的应用程序使用多面板布局来显示一个任务列表。每个Assignment可以放在一个AssignmentCategory中。我想使用DrawerLayout来显示所有的AssignmentCategories,使用户可以轻松地在不同类别之间切换。
但是我无法创建这样的布局。在DrawerLayout教程中,当用户点击一个项目(在我的情况下是一个AssignmentCategory)时,DrawerLayoutActivity将替换一个Fragment。而我使用的多面板布局需要一个FragmentActivity。我不知道如何创建一个包含多面板布局的Fragment。有人能做到吗?
1个回答

7

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

  • start from the multi pane demo project.
  • update the two activities of the multi pane demo to extends ActionBarActivity(v7), you don't need to extend FragmentActivity
  • implement the DrawerLayout(the sample code from the drawer project) code in the start list activity(I'm assuming you don't want the DrawerLayout in the details activity, but implementing it shouldn't be a problem if you want it).
  • the layout of the start list activity will be like this(don't forget that you need to implement the DrawerLayout changes in the activity_item_twopane.xml as well!):

    <DrawerLayout>
         <fragment android:id="@+id/item_list" .../>
         <ListView /> <!-- the list in the DrawerLayout-->
    </DrawerLayout>
    
  • change the implementation DrawerItemClickListener so when the user clicks the drawer list item you don't create and add a new list fragment, instead you update the single list fragment from the layout:

    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()]);
    }
    
  • the update method would be something like this:

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

    -various other small changes

我已经制作了一个样例项目来说明上述更改,你可以在这里找到。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接