用于导航的BaseActivity

Dun*_*gan 6 android navigation-drawer

我正在为导航构建一个基本活动,并且想要一些灵活的东西,因此Activity指示Base Activity要扩展哪个布局.

我有以下内容

public abstract class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private int mLayoutRes;

    protected void setLayout(int layoutRes) {
        mLayoutRes = layoutRes;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mLayoutRes);

        // Layout implements toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null){
            setSupportActionBar(toolbar);
        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        // The layout implements the nav
        if (drawer != null){
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }

    }
// Other Nav code ommitted as its too verbose
}
Run Code Online (Sandbox Code Playgroud)

然后布局从活动传递为folows

public class Home extends BaseActivity {

    private final String TAG = "Home";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.setLayout(R.layout.activity_home);
        super.onCreate(savedInstanceState);
        // Other Activity code
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来实现这一目标?也许设置一个内容框架的基础布局并膨胀到那个?

任何建议,将不胜感激.

Nam*_*ung 10

您可以从Google IO应用程序中关注BaseActivity.只是覆盖setContentView,你不需要setLayout

这是我的BaseActivity

package com.vtcmobile.kqviet.activity;


public class BaseActivity extends AppCompatActivity {

private Toolbar toolbar;

private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private NavigationView navigationView;
protected Context mContext;


public NavigationView getNavigationView() {
    return navigationView;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = BaseActivity.this;

}

@Override
public void setContentView(int layoutResID) {
    super.setContentView(layoutResID);
    initToolbar();
}

private void initToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

private void setUpNav() {
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    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);
}




 }
Run Code Online (Sandbox Code Playgroud)


BNK*_*BNK 7

你可以参考我的以下代码,注意addContentView我的基础活动(这里我说出来NavigationDrawerActivity)

基础活动:

public class NavigationDrawerActivity extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
        mDrawerLayout.setDrawerListener(toggle);
        toggle.syncState();
    }
    /**
     * called in extending activities instead of setContentView...
     *
     * @param layoutId The content Layout Id of extending activities
     */
    public void addContentView(int layoutId) {
        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(layoutId, null, false);
        mDrawerLayout.addView(contentView, 0);
    } 
}
Run Code Online (Sandbox Code Playgroud)

然后在其他活动中,例如,MainActivity:

public class MainActivity extends NavigationDrawerActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.addContentView(R.layout.activity_main);
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!


var*_*jsi 6

我使用基本活动的扩展布局和重用对BaseActivity类进行了一些更改,您可以使用以下类仅将其扩展到您的任何活动类中。

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);
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的base_activity.xml,

<android.support.v4.widget.DrawerLayout
android:id="@+id/activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"

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

</LinearLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    />
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)