MainActivity.java
public class MainActivity extends FragmentActivity {
public DrawerLayout mDrawerLayout;
public ListView mDrawerList;
public ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
public CharSequence mDrawerTitle;
// used to store app title
public CharSequence mTitle;
// slide menu items
public String[] navMenuTitles;
public TypedArray navMenuIcons;
public ArrayList<NavDrawerItem> navDrawerItems;
public NavDrawerListAdapter adapter;
ActionBar actionBar;
TextView tvTitle, tvTitleNext;
private Stack<Fragment> fragmentStack;
private Stack<Integer> fragmentCount;
private FragmentManager fragmentManager;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// startService(new Intent(getApplicationContext(),
// UpdateDeviceToken.class));
actionBar = getActionBar();
fragmentStack = new Stack<Fragment>();
fragmentCount = new Stack<Integer>();
fragmentManager = getSupportFragmentManager();
mTitle = mDrawerTitle = getResources().getString(R.string.app_name);
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(true));
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(1, -1)));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(2, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[8], navMenuIcons.getResourceId(3, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(this,
navDrawerItems);
mDrawerList.setAdapter(adapter);
mDrawerList.setSelection(0);
// enabling action bar app icon and behaving it as toggle button
// getActionBar().setDisplayHomeAsUpEnabled(true);
// getActionBar().setHomeButtonEnabled(true);
// LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// View v = inflator.inflate(R.layout.custom_t, null);
//
// actionBar.setCustomView(v);
actionBar.setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#F71D1D")));
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
// actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeButtonEnabled(true);
actionBar.setIcon(null);
View actionBarView = inflator.inflate(R.layout.custom_title, null);
tvTitle = (TextView)actionBarView.findViewById(R.id.title_text);
tvTitleNext = (TextView)actionBarView.findViewById(R.id.tvTitleNext);
tvTitle.setTypeface(KkpMandir.app_ver);
tvTitleNext.setTypeface(KkpMandir.app_ver);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_action_nav_menu, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
tvTitle.setText(mTitle);
//getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
tvTitle.setText(mDrawerTitle);
//getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
if (navDrawerItems.get(0).isImg()) {
displayView(1);
Log.e("MAIN---->", "POSITION: 1");
} else {
displayView(0);
Log.e("MAIN---->", "POSITION: 0");
}
}
actionBar.setCustomView(actionBarView);
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* *
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(false);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
public void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 1:
fragment = new HomeFragmentNew();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(R.id.frame_container, fragment);
if(fragmentStack.size()>0){
fragmentStack.lastElement().onPause();
ft.hide(fragmentStack.lastElement());
}
fragmentStack.push(fragment);
fragmentCount.push(position);
ft.commit();
Log.e("MAIN---->", "POSITION: 1");
break;
case 2:
fragment = new EventFragment();
FragmentTransaction ft1 = fragmentManager.beginTransaction();
ft1.add(R.id.frame_container, fragment);
fragmentStack.lastElement().onPause();
ft1.hide(fragmentStack.lastElement());
fragmentStack.push(fragment);
fragmentCount.push(position);
ft1.commit();
Log.e("MAIN---->", "POSITION: 2");
break;
case 3:
fragment = new AboutUsFragment();
FragmentTransaction ft2 = fragmentManager.beginTransaction();
ft2.add(R.id.frame_container, fragment);
fragmentStack.lastElement().onPause();
ft2.hide(fragmentStack.lastElement());
fragmentStack.push(fragment);
fragmentCount.push(position);
ft2.commit();
Log.e("MAIN---->", "POSITION: 3");
break;
case 4:
fragment = new OrganizationFragment();
FragmentTransaction ft3 = fragmentManager.beginTransaction();
ft3.add(R.id.frame_container, fragment);
fragmentStack.lastElement().onPause();
ft3.hide(fragmentStack.lastElement());
fragmentStack.push(fragment);
fragmentCount.push(position);
ft3.commit();
Log.e("MAIN---->", "POSITION: 4");
break;
case 5:
fragment = new PresidentMsgFragment();
FragmentTransaction ft4 = fragmentManager.beginTransaction();
ft4.add(R.id.frame_container, fragment);
fragmentStack.lastElement().onPause();
ft4.hide(fragmentStack.lastElement());
fragmentStack.push(fragment);
fragmentCount.push(position);
ft4.commit();
break;
case 6:
fragment = new NewsFragment();
FragmentTransaction ft5 = fragmentManager.beginTransaction();
ft5.add(R.id.frame_container, fragment);
fragmentStack.lastElement().onPause();
ft5.hide(fragmentStack.lastElement());
fragmentStack.push(fragment);
fragmentCount.push(position);
ft5.commit();
break;
case 7:
fragment = new VideoFragment();
FragmentTransaction ft6 = fragmentManager.beginTransaction();
ft6.add(R.id.frame_container, fragment);
fragmentStack.lastElement().onPause();
ft6.hide(fragmentStack.lastElement());
fragmentStack.push(fragment);
fragmentCount.push(position);
ft6.commit();
break;
case 8:
fragment = new GalleryFragment();
FragmentTransaction ft7 = fragmentManager.beginTransaction();
ft7.add(R.id.frame_container, fragment);
fragmentStack.lastElement().onPause();
ft7.hide(fragmentStack.lastElement());
fragmentStack.push(fragment);
fragmentCount.push(position);
ft7.commit();
break;
case 9:
fragment = new VoiceFragment();
FragmentTransaction ft8 = fragmentManager.beginTransaction();
ft8.add(R.id.frame_container, fragment);
fragmentStack.lastElement().onPause();
ft8.hide(fragmentStack.lastElement());
fragmentStack.push(fragment);
fragmentCount.push(position);
ft8.commit();
break;
default:
break;
}
// if (fragment != null) {
// FragmentManager fragmentManager = getSupportFragmentManager();
// fragmentManager.beginTransaction()
// .replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
// mDrawerList.setSelection(position);
setTitle(navMenuTitles[position-1]);
mDrawerLayout.closeDrawer(mDrawerList);
// error in creating fragment
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
tvTitle.setText(mTitle);
// getActionBar().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);
}
public void showExitDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle("Exit");
// set dialog message
alertDialogBuilder
.setMessage("Are you sure?")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
@Override
public void onBackPressed() {
if (fragmentStack.size() >1) {
FragmentTransaction ft = fragmentManager.beginTransaction();
fragmentCount.pop();
fragmentStack.lastElement().onPause();
ft.remove(fragmentStack.pop());
fragmentStack.lastElement().onResume();
ft.show(fragmentStack.lastElement());
ft.commit();
mDrawerList.setItemChecked((Integer)fragmentCount.lastElement(), true);
// mDrawerList.setSelection((Integer)fragmentCount.lastElement());
setTitle(navMenuTitles[(Integer)fragmentCount.lastElement()-1]);
} else {
showExitDialog();
}
}
}
以上是我的MainActivity文件代码。我用自定义操作栏实现简单的导航器。这就是为什么抽屉图标没有在某些设备中显示的原因。如果我使用默认操作栏比它工作正常。
以下是我的输出:
帮我解决这个错误。
答案 0 :(得分:13)
我通过替换:
解决了我的问题actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeButtonEnabled(true);
actionBar.setIcon(null);
使用:
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
actionBar.setIcon(android.R.color.transparent);
答案 1 :(得分:0)
这是更正确的答案。
根据日志警告,
W / ActionBarDrawerToggle(28554):DrawerToggle可能不会显示,因为导航图标不可见。您可能需要调用 actionbar.setDisplayHomeAsUpEnabled(true);