结合这两个项目不应该太困难。在示例代码中,DrawerLayout
示例替代了内容片段,但您不必执行相同的操作,只需更新相同的片段即可显示正确的数据。你可以这样实现这两个项目:
- 从多窗格演示项目开始。
- 更新多窗格演示的两项活动,以延伸
ActionBarActivity
(V7),你不需要延长FragmentActivity
- 实现
DrawerLayout
(从抽屉工程示例代码)代码在启动列表中的活动(我假设你不想在细节活动中使用DrawerLayout
,但是如果你需要的话,实现它应该不成问题)。
开始列表活动的布局将是这样的(不要忘记,你需要实现在activity_item_twopane.xml
的DrawerLayout
变化以及!):
<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;
}
- 各种其他小的变化。