creating base activity with navigation drawer in android

允我心安 提交于 2019-12-18 03:47:18

问题


I tried to follow this answer, with the main idea is to override the setContentView in the BaseActivity, the activity that will be extended by all activity so only BaseActivity will have the navigation drawer.

However, my navigation is never shown (even in BaseActivity) after i tried to implement the answer. This is what i did :

This is the navigation drawer's 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" >

    <!-- Framelayout to display Fragments -->

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

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

This is my overrided setContentView (in BaseActivity) :

@Override
    public void setContentView(int layoutResID) {
        // TODO Auto-generated method stub
        mDrawerLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null); // Your base layout here
        actContent= (FrameLayout) mDrawerLayout.findViewById(R.id.frame_container);
        //listLayout = (LinearLayout) mDrawerLayout.findViewById(R.id.listLayout);
        getLayoutInflater().inflate(layoutResID, actContent, true);
        //getLayoutInflater().inflate(layoutResID, listLayout, true);
        super.setContentView(layoutResID);
    }

And this is my onCreate on BaseActivity, note that i use getLayoutInflater().inflate instead of setContentView :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        //getSupportActionBar().setDisplayShowHomeEnabled(false);
        //getSupportActionBar().setDisplayShowCustomEnabled(true);
        // enabling action bar app icon and behaving it as toggle button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        //mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
        //mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
        mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));


        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }

    }

And finally, i just need to extend the BaseActivity class and code like usual.

UPDATE

According to the answer, this is my WORKING overrided setContentView :

@Override
    public void setContentView(final int layoutResID) {
        // TODO Auto-generated method stub
        mDrawerLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null); // Your base layout here
        actContent= (FrameLayout) mDrawerLayout.findViewById(R.id.frame_container);

        getLayoutInflater().inflate(layoutResID, actContent, true);
        super.setContentView(mDrawerLayout);

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));


        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        //super.setContentView(mDrawerLayout);
        /*getLayoutInflater().inflate(layoutResID, actContent, true);
        super.setContentView(mDrawerLayout);*/
    }

回答1:


Change your setContentView() like this...

@Override
public void setContentView(int layoutResID) {
    mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null); 
    actContent = (FrameLayout) mDrawerLayout.findViewById(R.id.frame_container);
    // set the drawer layout as main content view of Activity.
    setContentView(mDrawerLayout);
    // add layout of BaseActivities inside framelayout.i.e. frame_container
    getLayoutInflater().inflate(layoutResID, actContent, true);
}



回答2:


I know this is already answered, but in case someone struggles with my specific problem:

DrawerLayout can only have 2 childs, content and the navigationView. if you want to include a toolbar you should wrap this inside another layout or outside the drawerlayout:

DrawerLayout
--LinearLayout (or any other layout)
----Toolbar
----Content
--NavigationView

or:

LinearLayout (or any other layout)
--Toolbar
--DrawerLayout
----Content
----NavigationView


来源:https://stackoverflow.com/questions/22652556/creating-base-activity-with-navigation-drawer-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!