Android parent Navigation Drawer for all activities

こ雲淡風輕ζ 提交于 2019-11-30 16:00:46

Per the comments, in the extended Activity, we're simply finding the base class's DrawerLayout's content View, and inflating the extended Activity's layout into it.

public class MyActivity extends NavigationDrawerClass
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        ViewGroup content = (ViewGroup) findViewById(R.id.content_frame);
        getLayoutInflater().inflate(R.layout.my_activity, content, true);   
        ...
    }
}

You can use fragments in your activity. This is better solution.

But if you want to extend this activity you should set layout before call super.onCreate() method. And in your layout for every extended activity must add navigation drawer.

Let' example : In NavigationDrawerClass add method getLayout() and call in onCreate()

public class NavigationDrawerClass extend Activity {
      public void onCreate(Bundle saveInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(getLayout());
              // your code here
      }

      protected int getLayout() {
             return R.layout.navigation_drawer_class;
      }
}

In child activity use code like this

public class YourActivity extends NavigationDrawerClass{
    public void onCreate(Bundle saveInstanceState) {
         super.onCreate();
         // here do not call setContentView() method
         // all your other code
    }
    @Override
    protected int getLayout() {
         return R.layout.your_activity_xml_layout;
    }
}

An cleaner solution to @MikeM's solution is to override setContentView on the base class and do the trick there:

@Override
public void setContentView(int layoutResID)
{
    super.setContentView(R.layout.navigation_drawer_class);

    ViewGroup content = (ViewGroup) findViewById(R.id.content_frame);
    getLayoutInflater().inflate(layoutResID, content, true);
}

Then, on the activities that extends on, just call setContentView normally:

setContentView(R.layout.other_class);

EDIT: Also, on the base class, is necessary to do the initialization of the drawer, drawer toggle, etc. on the onPostCreate method instead on onCreate, to avoid calling setContentView two times (on the base onCreate and on the extended class onCreate):

@Override
protected void onPostCreate(Bundle savedInstanceState)
    super.onPostCreate(savedInstanceState);

    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    ListView list = (ListView) findViewById(R.id.left_drawer);
    list.setAdapter(new ArrayAdapter<String>(NavigationDrawerClass.this, android.R.layout.simple_list_item_1, names));
    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(
            NavigationDrawerClass.this,                  /* host Activity */
            drawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.open_drawer ,  /* "open drawer" description for accessibility */
            R.string.close_drawer  /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle("Drawer Closed");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle("Drawer Opened");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    drawerLayout.setDrawerListener(mDrawerToggle);
}

this work for me

public class MyDrawer extends AppCompatActivity {
ActionBarDrawerToggle toggle;
protected RelativeLayout fullLayout;
protected FrameLayout frameLayout;
@Override
public void setContentView(final int layoutResID) {
    fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.mydrawer, null);
    frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);
    getLayoutInflater().inflate(layoutResID, frameLayout, true);
    super.setContentView(fullLayout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout3);
    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            drawer.closeDrawers();
            int itemId = menuItem.getItemId();
            Toast.makeText(getApplicationContext(), menuItem.getTitle().toString(),
                    Toast.LENGTH_LONG).show();
            //navigationView.getMenu().findItem(R.id.drawer_5_reasons).setChecked(true);
            return true;
        }
    });
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    if (toggle.onOptionsItemSelected(item))
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/drawer_framelayout">
<FrameLayout
    android:id="@+id/drawer_frame2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<FrameLayout
    android:id="@+id/drawer_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main2"
app:menu="@menu/activity_main_drawer"
android:background="#fefefd" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

for use:

public class yourclass extends MyDrawer {

main problam .setOnItemClickListener

<android.support.v4.widget.DrawerLayout>
<FrameLayout>
    your main content stuff here
</android.support.v4.widget.DrawerLayout>
<FrameLayout>
    your main content stuff here(.setOnItemClickListener)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!