简体   繁体   中英

Navigation Drawer not showing up

So I have an old application. I wanted to add it a navigation drawer. So I want the navigation drawer to work with my current activity and to show ONLY on the currecnt activity

public class WebViewActivity extends Activity {

So the main activity is a webView.

<?xml version="1.0" encoding="utf-8"?>
<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">


<!--
      main content
 -->

<RelativeLayout

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#ffffff" >

    <WebView

        android:id="@+id/webView1"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:background="#ffffff" />

</RelativeLayout>

<!--
    navigation list item
-->
<FrameLayout

    android:id="@+id/content_frame"

    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="left"

    android:background="#2A323D"

    android:cacheColorHint="#00000000"

    android:choiceMode="singleChoice" />

</android.support.v4.widget.DrawerLayout>

So I expect my drawer to appear when I click the menu button or my webView detect the gesture left to right

    mTitle = mDrawerTitle = getTitle();
    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    drawerView = (View)findViewById(R.id.left_drawer);

    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPlanetTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

AND

        webView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x1 = event.getX();
                    break;
                case MotionEvent.ACTION_UP:
                    x2 = event.getX();
                    float deltaX = x2 - x1;
                    if (Math.abs(deltaX) > MIN_DISTANCE) {
                        mDrawerLayout.openDrawer(GravityCompat.START);
                        return false;
                    } else {
                        // consider as something else - a screen tap for example
                    }
                    break;
            }
            return true;
        }
    });

I really don't know what am I missing, but mDrawerLayout.openDrawer(GravityCompat.START); is not doing anything. There are no errors in the log. The suppossed drawer does not show up, nothing happens. I followed the official guide and more online. No luck.

I don't want to use Fragments!

UPDATE

The issue is that in my 'onCreate' I have something like: setContentView(webView);

So if I remove it, my navigation drawer works! BUT my webView does not. What am I missing?

从main_activity中删除FrameLayout,然后尝试

<?xml version="1.0" encoding="utf-8"?>
<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">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >

<WebView
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" />

</RelativeLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#2A323D"
    android:cacheColorHint="#00000000"
    android:choiceMode="singleChoice" />

</android.support.v4.widget.DrawerLayout>

Just copy and paste it

Your onCreate() must have setContentView(R.layout.drawer_layout); where drawer_layout is the xml which has a DrawerLayout as its root view

Follow the documentation Download sample code and see which xml is passed to setContentView()

If you follow it carefully you will notice activity_main.xml contains Drawerlayout and not the main content's xml.

Also keep in mind that DrawerLayout must contain exactly two child ViewGroup s, where the first can be a fragment or FrameLayout that populates a fragment at runtime AND the second must be NavigationView

If you are still confused you can follow this guide which has step-by-step instructions. Its worth a visit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM