我在Android中使用导航抽屉时遇到了一些问题。所以这些是我的代码:
public class NavigationDrawer extends FragmentActivity {
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;
protected DrawerLayout fullLayout;
protected FrameLayout actContent;
@Override
public void setContentView(final int layoutResID) {
fullLayout = (DrawerLayout) getLayoutInflater().inflate(
R.layout.navigation_drawer, null); // Your base layout here
actContent = (FrameLayout) fullLayout.findViewById(R.id.act_content);
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);
drawerListViewItems = getResources().getStringArray(R.array.items);
drawerListView = (ListView) findViewById(R.id.left_drawer);
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,
drawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
drawerListView.bringToFront();
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position,
long id) {
switch (position) {
}
drawerLayout.closeDrawer(drawerListView);
}
}
}
我的xml文件:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:divider="#CCCCCC"
android:dividerHeight="0.5dp"
android:paddingLeft="16dp"
android:paddingRight="16dp" />
<FrameLayout
android:id="@+id/act_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
字符串文件,我在其中声明导航抽屉项目的单词:
<string-array name="items">
<item>Dashboard</item>
<item>Account</item>
<item>Setting</item>
<item>Recurring</item>
<item>Budget</item>
</string-array>
但是使用这些代码,导航抽屉级别只有一个。我想要做的是在仪表板,设置和预算下还有子项目。
我想知道如何修改它,以便导航抽屉可以接收更多级别的项目。
提前致谢。
修改
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
// private ListView mDrawerList;
private ExpandableListView mDrawerList;
private LinearLayout navDrawerView;
CustomExpandAdapter customAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mAnalyzeEventSelection;
private int selectedPosition;
List<SampleTO> listParent;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
navDrawerView = (LinearLayout) findViewById(R.id.navDrawerView);
mAnalyzeEventSelection = getResources().getStringArray(R.array.analyzeEvent_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ExpandableListView) findViewById(R.id.nav_left_drawer);
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
listParent = new ArrayList<SampleTO>();
listDataChild = new HashMap<String, List<String>>();
// Navigation Drawer of Flight starts
listParent.add(new SampleTO(getString(R.string.createEventDrawer), R.drawable.sun));
listParent.add(new SampleTO(getString(R.string.analyzeEventDrawer), R.drawable.solar_system));
listParent.add(new SampleTO(getString(R.string.qrCodeDrawer), R.drawable.moon));
listDataChild.put(getString(R.string.createEventDrawer), new ArrayList<String>());
listDataChild.put(getString(R.string.qrCodeDrawer), new ArrayList<String>());
listDataChild.put(getString(R.string.analyzeEventDrawer), Arrays.asList(mAnalyzeEventSelection));
customAdapter = new CustomExpandAdapter(this, listParent, listDataChild);
// setting list adapter
mDrawerList.setAdapter(customAdapter);
mDrawerList.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
/* if (savedInstanceState == null) {
selectItem(0);
}*/
}
@Override
protected void onResume() {
super.onResume();
mDrawerList.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(groupPosition));
parent.setItemChecked(index, true);
String parentTitle = ((SampleTO) customAdapter.getGroup(groupPosition)).getTitle();
if (parentTitle.equals(getString(R.string.createEventDrawer))){
Toast.makeText(
MainActivity.this,
"Create Event",
Toast.LENGTH_LONG).show();
setTitle(mAnalyzeEventSelection[selectedPosition]);
}
else if (parentTitle != getString(R.string.analyzeEventDrawer)) {
mDrawerLayout.closeDrawer(navDrawerView);
}
return false;
}
});
mDrawerList.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
parent.setItemChecked(index, true);
selectItem(childPosition);
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content
// view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(navDrawerView);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void selectItem(int position) {
selectedPosition = position;
mDrawerLayout.closeDrawer(navDrawerView);
switch(selectedPosition){
case 0:
Toast.makeText(
MainActivity.this,
"Event Pop",
Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(
MainActivity.this,
"Buffer",
Toast.LENGTH_LONG).show();
break;
}
setTitle(mAnalyzeEventSelection[selectedPosition]);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
我的字符串文件:
<resources>
<string name="app_name">Navigation Drawer Example</string>
<string name="createEventDrawer">Create Event</string>
<string name="analyzeEventDrawer">Analyze Event</string>
<string-array name="analyzeEvent_array">
<item>Past Event Population</item>
<item>Buffer</item>
</string-array>
<string name="qrCodeDrawer">QR Code Scan</string>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>
<string name="action_websearch">Web search</string>
<string name="app_not_available">Sorry, there\'s no web browser available</string>
<string name="_11">11</string>