我有问题 动作栏抽屉切换.



最近我在我的应用程序中使用了这个功能。但是,使用ActionBarDrawer切换一个新应用程序,我在该功能中出现了未使用的错误。所以我实现了导入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);
                                                                    }
                                             };

肯定是它已经改变了语法时使用动作栏抽屉切换但找不到。

非常感谢你的一切

非常感谢你的一切。错误已解决。非常感谢@hata和@Spring破坏者

这是更正后的代码,以便其他人可以看到它

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);

相关内容

  • 没有找到相关文章

最新更新