1

我已阅读 NavigationDrawer 的文档并尝试在我自己的应用程序中创建它。但是有一个麻烦:应该用作菜单的 ListView 只是漂浮在主要内容上,我不能用它执行任何操作(例如通过滑动关闭)。问题是什么?

<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>

抽屉初始化:

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());

谢谢。

4

1 回答 1

2

这是一个对我有用的粗略代码示例,它们是来自android 开发者网站StylingAndroid 博客的东西的组合:

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);
}
于 2013-06-13T17:59:48.247 回答