如何在活动发布时显示“滑动菜单”? 我使用导航抽屉将幻灯片菜单显示为此代码
`public class MainActivity扩展Base_Activity { 私人DrawerLayout mDrawerLayout; public static ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; ImageView selectedicon;
Bitmap Member_Profile_Picture;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private TypedArray navMenuIconselected;
public static int selectedposition;
public static ArrayList<NavDrawerItem> navDrawerItems;
public static NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedicon = (ImageView) findViewById(R.id.icon);
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
//navMenuTitles=OffLine_Data.member.Get_Name();
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
navMenuIconselected = getResources().obtainTypedArray(
R.array.nav_drawer_icons_selected);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
/*navDrawerItems
.add(new NavDrawerItem(OffLine_Data.member.Get_Name(), navMenuIcons
.getResourceId(0, -1), navMenuIconselected
.getResourceId(0, -1)));*/
//Member_Profile_Picture
navDrawerItems
.add(new NavDrawerItem(OffLine_Data.member.Get_Name(),navMenuIconselected
.getResourceId(0, -1), navMenuIconselected
.getResourceId(0, -1)));
navDrawerItems
.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
.getResourceId(0, -1), navMenuIconselected
.getResourceId(0, -1)));
navDrawerItems
.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
.getResourceId(1, -1), navMenuIconselected
.getResourceId(1, -1)));
navDrawerItems
.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
.getResourceId(2, -1), navMenuIconselected
.getResourceId(2, -1)));
navDrawerItems
.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons
.getResourceId(3, -1), navMenuIconselected
.getResourceId(3, -1)));
navDrawerItems
.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons
.getResourceId(4, -1), navMenuIconselected
.getResourceId(4, -1)));
navDrawerItems
.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons
.getResourceId(5, -1), navMenuIconselected
.getResourceId(5, -1)));
navDrawerItems
.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons
.getResourceId(6, -1), navMenuIconselected
.getResourceId(6, -1)));
navDrawerItems
.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons
.getResourceId(7, -1), navMenuIconselected
.getResourceId(7, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, // 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) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
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
displayView(1);
}
}
/**
* 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(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
selectedposition = position;
break;
case 1:
fragment = new NewsFragment();
selectedposition = position;
break;
case 2:
fragment = new FootballTeamFragment();
selectedposition = position;
break;
case 3:
fragment = new SportsActivitiesFragment();
selectedposition = position;
break;
case 4:
fragment = new RestaurantsFragment();
selectedposition = position;
break;
case 5:
fragment = new EventsFragment();
selectedposition = position;
break;
case 6:
fragment = new TalkToCoachFragment();
selectedposition = position;
break;
case 7:
fragment = new FindMyChildFragment();
selectedposition = position;
break;
case 8:
fragment = new ProfileFragment();
selectedposition = position;
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
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);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems, selectedposition);
mDrawerList.setAdapter(adapter);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
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);
}
} `
答案 0 :(得分:0)
在初始化抽屉后添加下一个片段
打开抽屉:
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT))
mDrawerLayout.openDrawer(Gravity.LEFT) //for left drawer
关闭抽屉:
if (!mDrawerLayout.isDrawerOpen(Gravity.LEFT))
mDrawerLayout.closeDrawer(Gravity.LEFT) //for left drawer
答案 1 :(得分:0)
您可以使用:(打开左侧的抽屉)
DrawerLayout.openDrawer(Gravity.LEFT);