DrawerLayout and Multi Pane Layout

核能气质少年 提交于 2019-12-05 03:11:43

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:

  • 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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!