DrawerLayout和多窗格布局 [英] DrawerLayout and Multi Pane Layout

查看:112
本文介绍了DrawerLayout和多窗格布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用多窗格布局以显示任务列表。每个分配可以放在一个 AssignmentCategory 。我想用一个<一个href="http://developer.android.com/training/implementing-navigation/nav-drawer.html">DrawerLayout显示所有AssignmentCategories使用户可以在向度类别之间轻松切换。

My application uses a Multi Pane layout to display a list of assignments. Each Assignment can be put in one AssignmentCategory. I want to use a DrawerLayout to display all the AssignmentCategories so the user can switch easily between the diffirent categories.

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

I didn't manage to create such a layout. In the official DrawerLayout tutorial the DrawerLayoutActivity replaces a Fragment when a user clicks on a item (in my case an AssignmentCategory). The problem I facing is that a Multi Pane layout requires a FragmentActivity. I don't know how to create a Fragment which contains a Multi Pane layout. Did someone manage to do this?

推荐答案

结合这两个项目应该不会太困难。在C DrawerLayout 例如样品$ C $不会取代内容片段,但你不必这样做,你可以简单地更新相同的片段,以显示正确的数据。你可以实现两个项目是这样的:

Combining the two projects shouldn't be too difficult. In the sample code the DrawerLayout example does replace the content fragment but you don't have to do the same, you could simply update the same fragment to show the proper data. You could implement the two projects this way:

  • 从多窗格示范项目启动。
  • 更新多窗格中演示的两项活动,以延伸 ActionBarActivity (第7版),你并不需要延长 FragmentActivity
  • 实施 DrawerLayout 在启动列表活动(样品$ C $从抽屉项目C)code(我假设你不想在 DrawerLayout 在细节上活动,但实现它不应该是一个问题,如果你想的话)。
  • 开始列表活动的布局将是这样的(不要忘记,你需要实现 DrawerLayout 变化 activity_item_twopane.xml 和):

  • 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>

  • 改实施 DrawerItemClickListener 因此,当用户单击抽屉列表项目不创建并添加一个新的列表片段,而不是更新的单一列表从布局片段:

  • 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

    我做了一个样本项目来说明,你可以找到这里上述变化

    I've made a sample project to illustrate the above changes that you can find here.

    这篇关于DrawerLayout和多窗格布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆