我有一个基本问题,即过去几天我无法解决(即使在google搜索和SOing中有很多例子/解释之后)。
我有5个不同的Activity,每个活动都独立于其他活动,每个活动都有自己的布局文件。现在,我想添加导航抽屉作为我的应用程序菜单。
据我所知,推荐的两种方法是:
1)将这5个活动合并为一个MainActivity,并使用5个片段加载5个不同的布局。但是我很难将这些5合并为1.如果我在一个Activity中加载了所有方法,那么不会出现任何性能问题吗?
2)让一个BaseActivity包含抽屉,并将所有其他活动扩展到Base Activity。但我将所有活动扩展到NavDrawer类(包含抽屉),但不起作用。单独的抽屉工作正常(当我只运行NavDrawer时)。我是否需要对现有活动的布局xmls进行任何更改?
我很抱歉,如果这是非常基本的,但是在我未能在2天内掌握这个概念后,我发布了这个!
如果你愿意,我可以附上我的代码,但根据教程,抽屉代码是基本的东西。
谢谢,
我正在使用的参考文献:
1)http://developer.android.com/training/implementing-navigation/nav-drawer.html
2)http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
答案 0 :(得分:1)
我建议您将ViewPager
与actionbar tabs
一起使用。因为在viewpager的帮助下,所有五个片段都可以看到相同的导航draawer。为了提高效率,ViewPager
创建了当前页面,此外它还会为您的下一页和上一页做好准备。因此,当用户滑动页面时,它可以快速显示内容。我总是遵循同样的原则,我不认为这是低效的。
我的某个应用程序的代码段如下:
public class MainActivity extends ActionBarActivity {
String[] titles;
ViewPager viewPager;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mListTitles;
public PagerTabStrip titleStrip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewPager = (ViewPager) findViewById(R.id.pager);
TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager());
viewPager.setAdapter(titleAdapter);
viewPager.setCurrentItem(0);
mTitle = mDrawerTitle = getTitle();
mListTitles = getResources().getStringArray(R.array.drawerlist_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// sets up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mListTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enables ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
)
{
public void onDrawerClosed(View view)
{
getSupportActionBar().setTitle(mTitle);
}
public void onDrawerOpened(View drawerView)
{
getSupportActionBar().setTitle(mDrawerTitle);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null)
{
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
else{
return false;
}
}
class TitleAdapter extends FragmentPagerAdapter{
private String titles[] = new String[]{"Expenses","Savings","Income"};
private Fragment frags[] = new Fragment[titles.length];
public TitleAdapter(FragmentManager fm) {
super(fm);
frags[0] = new Fragment1();
frags[1] = new Fragment2();
frags[2] = new Fragment3();
}
@Override
public CharSequence getPageTitle (int position){
Log.v("TitleAdapter - getPageTitle=", titles[position]);
return titles[position];
}
@Override
public Fragment getItem(int position) {
Log.v("TitleAdapter - getItem=", String.valueOf(position));
return frags[position];
}
@Override
public int getCount() {
return frags.length;
}
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
selectItem(position);
}
}
private void selectItem(int position)
{
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mListTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title)
{
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}