BaseActivity for Navigation

前端 未结 6 1747
猫巷女王i
猫巷女王i 2020-12-24 09:21

I\'m building a base activity for navigation and want something flexible so the Activity dictates to the Base Activity which layout to inflate.

I have the following<

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 10:18

    I make some changes in BaseActivity class using inflating layout of baseactivity and reuse, you can use following class for extend only into any one of your activity class.

    import android.content.Context;
    import android.content.res.Configuration;
    import android.os.Bundle;
    import android.support.design.widget.NavigationView;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.FrameLayout;
    
    
    public class BaseActivity extends AppCompatActivity {
    
    public Toolbar toolbar;
    
    public DrawerLayout drawerLayout;
    public ActionBarDrawerToggle drawerToggle;
    public NavigationView navigationView;
    public Context mContext;
    public NavigationView getNavigationView() {
        return navigationView;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = BaseActivity.this;
        setContentView(R.layout.base_activity);
    
    }
    
    @Override
    public void setContentView(int layoutResID) {
        DrawerLayout fullView = (DrawerLayout)getLayoutInflater().inflate(R.layout.base_activity, null);
        FrameLayout activityContainer = (FrameLayout) fullView.findViewById(R.id.activity_content);
        getLayoutInflater().inflate(layoutResID, activityContainer, true);
        super.setContentView(fullView);
    
        initToolbar();
    }
    
    private void initToolbar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
    
    private void setUpNav() {
        drawerLayout = (DrawerLayout) findViewById(R.id.activity_container);
        drawerToggle = new ActionBarDrawerToggle(BaseActivity.this, drawerLayout, R.string.app_name, R.string.app_name);
        drawerLayout.setDrawerListener(drawerToggle);
    
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    
        navigationView = (NavigationView) findViewById(R.id.navigation);
    
    
        // Setting Navigation View Item Selected Listener to handle the item
        // click of the navigation menu
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    
            public boolean onNavigationItemSelected(MenuItem menuItem) {
    
                // Checking if the item is in checked state or not, if not make
                // it in checked state
                if (menuItem.isChecked())
                    menuItem.setChecked(false);
                else
                    menuItem.setChecked(true);
    
                // Closing drawer on item click
                drawerLayout.closeDrawers();
    
                // Check to see which item was being clicked and perform
                // appropriate action
    
                Intent intent;
                switch (menuItem.getItemId()) {
                    case R.id.xxxx:
                        return true;
                }
                return false;
            }
        });
    
        // Setting the actionbarToggle to drawer layout
    
        // calling sync state is necessay or else your hamburger icon wont show
        // up
        drawerToggle.syncState();
    
    }
    
    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        setUpNav();
    
        drawerToggle.syncState();
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (drawerToggle.onOptionsItemSelected(item))
            return true;
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    }
    

    This is my base_activity.xml,

    
    
    
        
        
    
    
    
    
    

提交回复
热议问题