本文介绍了导航的BaseActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在建立一个用于导航的基础活动,并且想要一些灵活的方法,因此活动将指示基础活动膨胀的基础布局.
I'm building a base activity for navigation and want something flexible so the Activity dictates to the Base Activity which layout to inflate.
我有以下内容
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
}
然后将布局作为以下内容从活动中传递
Then the Layout is passed from the activity as 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
}
}
是否有更好的方法来实现这一目标?也许使用内容框架设置基本布局并将其夸大了?
Is there a better way to achieve this?Maybe setting a base layout with a content frame and inflating into that?
任何建议将不胜感激.
推荐答案
您可以关注 BaseActivity .只需覆盖setContentView
,就不需要setLayout
You can follow BaseActivity from Google IO application. Just override setContentView
and you dont need setLayout
这是我的BaseActivity
Here is my 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);
}
}
这篇关于导航的BaseActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!