我正在開發Android項目,並試圖使用Sliding Navigation Drawer來實現ActionBarSherlock。使用ActionBarSherlock實現滑動導航抽屜
實際的菜單工作正常但是,3行圖標(我想如果我沒有記錯它稱爲漢堡圖標)不會出現,而不是回到父母的圖標,但點擊它顯示/隱藏菜單。
下面是我的代碼:
public class MainActivity extends SherlockFragmentActivity {
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
Fragment frag = new MainFragment();
t.replace(R.id.content_frame, frag);
t.commit();
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList = (ListView)findViewById(R.id.listview_drawer);
NavigationManagerAdapter menuAdapter = new NavigationManagerAdapter(MainActivity.this);
menuAdapter.add(new NavigationMenuItem("MenuItem", "MENU_PRODUCTS", GuiType.TEXTVIEW, null));
menuAdapter.add(new NavigationMenuItem("MenuItem2", "MENU_TEST", GuiType.TEXTVIEW_SUMMARY, "This is a summary", mSpecificClickListener));
mDrawerList.setAdapter(menuAdapter);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
// TODO Auto-generated method stub
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
// Set the title on the action when drawer open
getSupportActionBar().setTitle("hello");
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList.setOnItemClickListener(new DrawerClickListener());
}
public OnClickListener mSpecificClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Specific click listener used", Toast.LENGTH_LONG).show();
}
};
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(item);
}
@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 toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
感謝您的幫助,您可以提供。如果你使用ActionBarCompat的代替ActionBarSherlock因爲ABS沒有這種支持
即使我想,這似乎並沒有改變任何東西,嘗試'在onOptionsItemSelected返回TRUE'。此外,你可以使用這個庫,它允許你在ABS左上角顯示一個圖標:https://github.com/sockeqwe/SlidingMenu HTH – Fllo