NavigationDrawer不工作 [英] NavigationDrawer is not working

查看:128
本文介绍了NavigationDrawer不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了NavigationDrawer的文档,并试图在自己的应用程序来创建它。但是有一个麻烦:即应作为菜单只是浮在主要内容和我不能执行任何与它的动作(例如,通过刷卡关闭)的ListView。有什么问题?

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>

和抽屉初始化:

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

感谢。

推荐答案

下面是$一个粗略的样本C $ CS的工作对我来说,他们是从的 Android开发者网站和的 StylingAndroid博客

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

这篇关于NavigationDrawer不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆