pin*_*yid 3 android listview android-layout
我正在尝试实现两级DrawerLayout.作为基准,我正在使用http://developer.android.com/training/implementing-navigation/nav-drawer.html上的Google星球示例.所以我想扩展这个例子,这样从行星列表中选择一个行星,用行星上的城市列表替换行星列表(除了地球以外大多空白 - 我仍在充实我的数据: - )).
我想我可以采取三种方法: -
所以尝试选项3(这是最好的方法吗?),在我的DrawerItemClickListener中,我执行以下操作...
arrayAdapter.clear();
arrayAdapter.addAll(arrayListOfCities);
arrayAdapter.notifyDataSetChanged();
mDrawerList.invalidateViews();
mDrawerList.forceLayout();
mDrawerList.refreshDrawableState();
Run Code Online (Sandbox Code Playgroud)
但它似乎没有用,即行星列表不会被城市列表所取代.
我的方法是否正确?如果是,我如何让列表刷新?
替换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>
Run Code Online (Sandbox Code Playgroud)
创建方法loadContentList(int)在MainActivity.这是在主菜单点击项目时调用的方法:
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);
}
// Change ListView's adapter
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, content));
// Change OnItemClickListener // CityItemClickListener is defined below
mDrawerList.setOnItemClickListener(new CityItemClickListener());
}
Run Code Online (Sandbox Code Playgroud)
由于我们想要在点击行星项目时更新ListView,我们将更改DrawerItemClickListener的方法体并调用新添加的loadContentList(int):
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//selectItem(position);
loadContentList(position);
}
}
Run Code Online (Sandbox Code Playgroud)
单击一个城市时,我们将需要另一个OnItemClickListener来更改片段.:
private class CityItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// This is the method that was being called on planet click
// in the original example. Implementation of it is up to you
selectItem(position);
}
}
Run Code Online (Sandbox Code Playgroud)
保留ListView和ArrayAdapter,但替换适配器的数据
定义ArrayAdapter:
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
---------> private ArrayAdapter<String> mAdapter;
Run Code Online (Sandbox Code Playgroud)
在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);
Run Code Online (Sandbox Code Playgroud)
我们必须传递一个ArrayList以避免UnsupportedOperationException在调用时mAdapter.clear().
更改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);
// update
mAdapter.notifyDataSetChanged();
// Change on item click listener
mDrawerList.setOnItemClickListener(new CityItemClickListener());
}
Run Code Online (Sandbox Code Playgroud)
CityItemClickListener和DrawerItemClickListener将保持原样.唯一的变化是:我们没有在行星列表项目上创建新的ArrayAdapter点击.
您需要找出一种方法(可能是一个按钮),以便用户从城市视图返回到行星列表.
我还建议您查看ExpandableListView.它可以更优雅地解决您的问题.
| 归档时间: |
|
| 查看次数: |
1741 次 |
| 最近记录: |