问题
I have read the documentation for NavigationDrawer and tried to create it in my own application. But there is a trouble: the ListView that should be used as menu just floats over the main content and I can't perform any actions with it (e.g. close by swipe). What is the problem?
<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" >
<LinearLayout
android:id="@+id/mainPageLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<android.support.v4.view.ViewPager
android:id="@+id/mainScreenViewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<android.support.v4.view.PagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
<ListView
android:id="@+id/navigationDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:gravity="start" >
</ListView>
</android.support.v4.widget.DrawerLayout>
And the drawer initialization:
String[] titles = getResources().getStringArray(R.array.lists_titles);
ListView drawer = (ListView) findViewById(R.id.navigationDrawer);
drawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles));
drawer.setOnItemClickListener(new DrawerItemClickListener());
Thanks.
回答1:
Here is a rough sample of codes that work for me, they're a combination of things from android developers site and StylingAndroid blog:
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layoutxml_id);
// must initialize mDrawerLayout and mDrawerToggle in main thread
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
// do something
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
// do something
}
};
String[] titles = getResources().getStringArray(R.array.lists_titles);
ListView drawer = (ListView) findViewById(R.id.navigationDrawer);
drawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles));
drawer.setOnItemClickListener(new DrawerItemClickListener());
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeButtonEnabled(true);
}
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
if(item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
return super.onOptionsItemSelected(item);
}
来源:https://stackoverflow.com/questions/17093545/navigationdrawer-is-not-working