最近我在我的应用程序中使用了这个功能。 但是,使用ActionBarDrawerToggle一个新的应用程序,我在该函数中没有使用错误。 所以我实现了import android.support.v7.app.ActionBarDrawerToggle;但现在代码对我来说不起作用。
public class MainActivity extends ActionBarActivity {
private ListView drawerList;
private String[] drawerOptions;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private Fragment[] fragments = new Fragment[]{
new MainFragment(),
new AcordesFragment(),
new AfinadorFragment(),
new AboutFragment(),
new ExitFragment(),
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerList = (ListView) findViewById(R.id.leftDrawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerOptions = getResources().getStringArray(R.array.drawer_options);
drawerList.setAdapter(new ArrayAdapter<String> (this,
R.layout.drawer_list_item,
drawerOptions));
drawerList.setItemChecked(0, true);
drawerList.setOnItemClickListener(new DrawerItemClickListener());
drawerToggle = new ActionBarDrawerToggle(this,
drawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close){
public void onDrawerClosed(View view){
ActivityCompat.invalidateOptionsMenu(MainActivity.this);
}
public void onDrawerOpened(View drawerView){
ActivityCompat.invalidateOptionsMenu(MainActivity.this);
}
};
drawerLayout.setDrawerListener(drawerToggle);
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.add(R.id.contentFrame, fragments[0])
.add(R.id.contentFrame, fragments[1])
.add(R.id.contentFrame, fragments[2])
.add(R.id.contentFrame, fragments[3])
.add(R.id.contentFrame, fragments[4])
.hide(fragments[1])
.hide(fragments[2])
.hide(fragments[3])
.hide(fragments[4])
.commit();
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeButtonEnabled(true);
if( getIntent().getBooleanExtra("Exit me", false)){
finish();
return; // add this to prevent from doing unnecessary stuffs
}
}
错误在以下代码中:
drawerList.setOnItemClickListener(new DrawerItemClickListener());
drawerToggle = new ActionBarDrawerToggle(this,
drawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close){
public void onDrawerClosed(View view){
ActivityCompat.invalidateOptionsMenu(MainActivity.this);
}
public void onDrawerOpened(View drawerView){
ActivityCompat.invalidateOptionsMenu(MainActivity.this);
}
};
当然是它改变了语法当使用ActionBarDrawerToggle但找不到时。
非常感谢你的一切
答案 0 :(得分:0)
非常感谢你的一切。 该错误已得到解决。 非常感谢@hata和@Spring Breaker
以下是更正的代码,以便其他人可以看到
public class MainActivity extends ActionBarActivity {
private ListView drawerList;
private String[] drawerOptions;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private Fragment[] fragments = new Fragment[]{
new MainFragment(),
new AcordesFragment(),
new AfinadorFragment(),
new AboutFragment(),
new ExitFragment(),
};
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerList = (ListView) findViewById(R.id.leftDrawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerOptions = getResources().getStringArray(R.array.drawer_options);
drawerList.setAdapter(new ArrayAdapter<String> (this,
R.layout.drawer_list_item,
drawerOptions));
drawerList.setItemChecked(0, true);
drawerList.setOnItemClickListener(new DrawerItemClickListener());
drawerToggle = new ActionBarDrawerToggle(this,
drawerLayout,
//R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close){
public void onDrawerClosed(View view){
ActivityCompat.invalidateOptionsMenu(MainActivity.this);
}
public void onDrawerOpened(View drawerView){
ActivityCompat.invalidateOptionsMenu(MainActivity.this);
}
};
drawerLayout.setDrawerListener(drawerToggle);