替换ListView
实际上没有必要这样做。
保留ListView,但替换其ArrayAdapter
这是最容易实现的方法。首先,在res/values/strings.xml中添加以下字符串数组(城市名称):
<string-array name="mercury_array">
<item>Undiscovered City 1 (Mercury)</item>
<item>Undiscovered City 2 (Mercury)</item>
<item>Undiscovered City 3 (Mercury)</item>
<item>Undiscovered City 4 (Mercury)</item>
</string-array>
<string-array name="venus_array">
<item>Undiscovered City 1 (Venus)</item>
<item>Undiscovered City 2 (Venus)</item>
<item>Undiscovered City 3 (Venus)</item>
<item>Undiscovered City 4 (Venus)</item>
</string-array>
<string-array name="earth_array">
<item>New York</item>
<item>Hong Kong</item>
<item>New Delhi</item>
<item>London</item>
</string-array>
<string-array name="mars_array">
<item>Undiscovered City 1 (Mars)</item>
<item>Undiscovered City 2 (Mars)</item>
<item>Undiscovered City 3 (Mars)</item>
<item>Undiscovered City 4 (Mars)</item>
</string-array>
<string-array name="jupiter_array">
<item>Undiscovered City 1 (Jupiter)</item>
<item>Undiscovered City 2 (Jupiter)</item>
<item>Undiscovered City 3 (Jupiter)</item>
<item>Undiscovered City 4 (Jupiter)</item>
</string-array>
<string-array name="saturn_array">
<item>Undiscovered City 1 (Saturn)</item>
<item>Undiscovered City 2 (Saturn)</item>
<item>Undiscovered City 3 (Saturn)</item>
<item>Undiscovered City 4 (Saturn)</item>
</string-array>
<string-array name="uranus_array">
<item>Undiscovered City 1 (Uranus)</item>
<item>Undiscovered City 2 (Uranus)</item>
<item>Undiscovered City 3 (Uranus)</item>
<item>Undiscovered City 4 (Uranus)</item>
</string-array>
<string-array name="neptune_array">
<item>Undiscovered City 1 (Neptune)</item>
<item>Undiscovered City 2 (Neptune)</item>
<item>Undiscovered City 3 (Neptune)</item>
<item>Undiscovered City 4 (Neptune)</item>
</string-array>
在
MainActivity
中创建一个名为
loadContentList(int)
的方法。这个方法将在主列表项点击时被调用:
private void loadContentList(int position) {
setTitle(mPlanetTitles[position]);
String[] content;
switch(position) {
case 0:
content = getResources().getStringArray(R.array.mercury_array);
break;
case 1:
content = getResources().getStringArray(R.array.venus_array);
break;
case 2:
content = getResources().getStringArray(R.array.earth_array);
break;
case 3:
content = getResources().getStringArray(R.array.mars_array);
break;
case 4:
content = getResources().getStringArray(R.array.jupiter_array);
break;
case 5:
content = getResources().getStringArray(R.array.saturn_array);
break;
case 6:
content = getResources().getStringArray(R.array.uranus_array);
break;
case 7:
content = getResources().getStringArray(R.array.neptune_array);
break;
default:
content = getResources().getStringArray(R.array.neptune_array);
}
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, content));
mDrawerList.setOnItemClickListener(new CityItemClickListener());
}
由于我们希望在单击星球项目时更新ListView,因此我们将更改DrawerItemClickListener的方法体并调用新添加的loadContentList(int)
:
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
loadContentList(position);
}
}
我们需要另一个OnItemClickListener来在单击城市时更改片段。
private class CityItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
保留ListView和ArrayAdapter,但替换适配器的数据。
定义ArrayAdapter:
private ListView mDrawerList
private ActionBarDrawerToggle mDrawerToggle
---------> private ArrayAdapter<String> mAdapter
在MainActivity的onCreate(Bundle)方法中:
ArrayList<String> planetList = new ArrayList<String>()
planetList.addAll(Arrays.asList(mPlanetTitles))
mAdapter = new ArrayAdapter<String>(this,
R.layout.drawer_list_item, planetList)
// set up the drawer's list view with items
mDrawerList.setAdapter(mAdapter)
我们需要传递一个ArrayList,这样在调用mAdapter.clear()
时就可以避免UnsupportedOperationException
异常。
将loadContentList(int)
修改为以下内容:
private void loadContentList2(int position) {
setTitle(mPlanetTitles[position]);
String[] content;
switch(position) {
....
....
}
ArrayList<String> cityList = new ArrayList<String>();
cityList.addAll(Arrays.asList(content));
mAdapter.clear();
mAdapter.addAll(cityList);
mAdapter.notifyDataSetChanged();
mDrawerList.setOnItemClickListener(new CityItemClickListener());
}
CityItemClickListener和DrawerItemClickListener将保持与以前相同。唯一的变化是:我们不会在点击星球列表项时创建新的ArrayAdapter。
您需要想出一种方式(例如按钮),让用户从城市视图返回星球列表。
我还建议您研究ExpandableListView。它可能更优雅地解决您的问题。