我想在导航抽屉中动态填充一个可扩展的listview。我正在使用树形图来管理数据,但是只有当我具有相同数量的菜单和子菜单时,它才起作用。如果不是,则不显示所有子菜单。例如,如果我有3个菜单和6个子菜单,它将仅显示3个菜单和3个子菜单。这些是我的方法:
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close)
{
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("MiningTag");
invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private void addDrawersItem() {
adapter=new CustomExpandableListAdapter(this,lstTitle,lstChild);
expandableListView.setAdapter(adapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
getSupportActionBar().setTitle(lstTitle.get(groupPosition).toString());
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
getSupportActionBar().setTitle("MiningTag");
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
String selectedItem = ((List)(lstChild.get(lstTitle.get(groupPosition))))
.get(childPosition).toString();
getSupportActionBar().setTitle(selectedItem);
if(items[0].equals(lstTitle.get(groupPosition)))
navigationManager.showFragment(selectedItem);
else
throw new IllegalArgumentException("Not Supported Fragment");
mDrawerLayout.closeDrawer(GravityCompat.START);
return false;
}
});
}
private void genData() throws IOException {
List<String> title = Arrays.asList("Mina El Teniente");
//List<String> childItem = Arrays.asList("Pilar","Reno");
List<String> childItem = new ArrayList<String>();
String linea;
InputStream is = this.getResources().openRawResource(R.raw.data);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if(is!=null){
while((linea=reader.readLine())!=null){
childItem.add(linea);
}
}
is.close();
Toast.makeText(this, "carga: "+childItem.size(), Toast.LENGTH_LONG).show();
lstChild=new TreeMap<>();
lstChild.put(title.get(0),childItem);
lstTitle = new ArrayList<>(lstChild.keySet());
}