我的应用程序使用多窗格布局 http://developer.android.com/design/patterns/multi-pane-layouts.html显示作业列表。每个Assignment
可以放在一个AssignmentCategory
。我想用一个抽屉布局 http://developer.android.com/training/implementing-navigation/nav-drawer.html显示所有分配类别,以便用户可以在不同类别之间轻松切换。
我没能创建这样的布局。在官方抽屉布局教程 http://developer.android.com/training/implementing-navigation/nav-drawer.htmlDrawerLayoutActivity 替换了Fragment
当用户点击一个项目时(在我的例子中是AssignmentCategory
)。我面临的问题是多窗格布局需要FragmentActivity
。我不知道如何创建一个Fragment
其中包含多窗格布局。有人设法做到这一点吗?
将这两个项目结合起来应该不会太困难。在示例代码中DrawerLayout
示例确实替换了内容片段,但您不必执行相同的操作,您可以简单地更新相同的片段以显示正确的数据。您可以这样实施这两个项目:
- 从多窗格演示项目开始。
- 将多窗格演示的两个活动更新为扩展
ActionBarActivity
(v7),你不需要扩展FragmentActivity
- 实施
DrawerLayout
(抽屉项目中的示例代码)开始列表活动中的代码(我假设您不希望DrawerLayout
在详细信息活动中,但如果您愿意的话,实施它应该不是问题)。
-
开始列表活动的布局将是这样的(不要忘记您需要实现DrawerLayout
的变化activity_item_twopane.xml
还有!):
<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;
}
-其他各种小变化
我制作了一个示例项目来说明您可以进行的上述更改在这里找到 https://github.com/luksprog/sasp/tree/master/app/MultipaneDrawerLayout.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)