打开时抽屉图标不会变为箭头
Drawer icon not changing to the arrow when opened
我只是在学习 Android 遵循 Head First:Android 书,在第 10 章:抽屉中,它展示了如何创建和处理抽屉,我遵循了所有内容,但正如书中所示一旦用户打开抽屉,汉堡包图标应该变为后退箭头,我的抽屉打开并且点击工作正常,唯一的是图标不会变为后退箭头,即使当我打开抽屉,这是我的代码:
public class MainActivity extends Activity {
private String[] titles;
private ListView drawerList;
private ShareActionProvider shareActionProvider;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titles = getResources().getStringArray(R.array.titles);
drawerList = (ListView) findViewById(R.id.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, titles));
drawerList.setOnItemClickListener(new DrawerItemClickListener());
if(savedInstanceState == null){
selectItem(0);
}
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open_drawer, R.string.close_drawer){
public void onDrawerClosed(View view){
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View view){
super.onDrawerOpened(view);
invalidateOptionsMenu();
}
};
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
boolean drawerOpen = drawerLayout.isDrawerOpen(drawerList);
menu.findItem(R.id.action_share).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
setIntent("Text");
return super.onCreateOptionsMenu(menu);
}
private void setIntent(String text){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text);
shareActionProvider.setShareIntent(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
if(drawerToggle.onOptionsItemSelected(item)){
return true;
}
switch (item.getItemId()){
case R.id.action_create_order:
Intent intent = new Intent(this, OrderActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
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){
Fragment fragment;
switch (position){
case 1:
fragment = new PizzaFragment();
break;
case 2:
fragment = new PastaFragment();
break;
case 3:
fragment = new StoresFragment();
break;
default:
fragment = new TopFragment();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
ft.commit();
setActionBarTitle(position);
drawerLayout.closeDrawer(drawerList);
}
private void setActionBarTitle(int position){
String title;
if(position == 0){
title = getResources().getString(R.string.app_name);
}else{
title = titles[position];
}
getActionBar().setTitle(title);
}
}
初始化抽屉开关后,设置抽屉侦听器,因此:
drawerLayout.setDrawerListener(drawerToggle);
我只是在学习 Android 遵循 Head First:Android 书,在第 10 章:抽屉中,它展示了如何创建和处理抽屉,我遵循了所有内容,但正如书中所示一旦用户打开抽屉,汉堡包图标应该变为后退箭头,我的抽屉打开并且点击工作正常,唯一的是图标不会变为后退箭头,即使当我打开抽屉,这是我的代码:
public class MainActivity extends Activity {
private String[] titles;
private ListView drawerList;
private ShareActionProvider shareActionProvider;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titles = getResources().getStringArray(R.array.titles);
drawerList = (ListView) findViewById(R.id.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, titles));
drawerList.setOnItemClickListener(new DrawerItemClickListener());
if(savedInstanceState == null){
selectItem(0);
}
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open_drawer, R.string.close_drawer){
public void onDrawerClosed(View view){
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View view){
super.onDrawerOpened(view);
invalidateOptionsMenu();
}
};
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
boolean drawerOpen = drawerLayout.isDrawerOpen(drawerList);
menu.findItem(R.id.action_share).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
setIntent("Text");
return super.onCreateOptionsMenu(menu);
}
private void setIntent(String text){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text);
shareActionProvider.setShareIntent(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
if(drawerToggle.onOptionsItemSelected(item)){
return true;
}
switch (item.getItemId()){
case R.id.action_create_order:
Intent intent = new Intent(this, OrderActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
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){
Fragment fragment;
switch (position){
case 1:
fragment = new PizzaFragment();
break;
case 2:
fragment = new PastaFragment();
break;
case 3:
fragment = new StoresFragment();
break;
default:
fragment = new TopFragment();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
ft.commit();
setActionBarTitle(position);
drawerLayout.closeDrawer(drawerList);
}
private void setActionBarTitle(int position){
String title;
if(position == 0){
title = getResources().getString(R.string.app_name);
}else{
title = titles[position];
}
getActionBar().setTitle(title);
}
}
初始化抽屉开关后,设置抽屉侦听器,因此:
drawerLayout.setDrawerListener(drawerToggle);