Android上的ActionBarDrawerToggle问题

时间:2014-11-01 23:08:52

标签: android

好吧,伙计们,我正在为我的工作开发一个私人个人应用程序,但我有一个未知的问题,我有点迷失这个,不知道这是怎么回事,我和#39;我试图设置一个简单的ActionBarDrawerToggle,但它没有工作,它甚至显示和取消设置Icon而不是我设置,所以请,任何人都可以帮助我吗?如果我不解决这个问题,我无法继续前进

public class MainActivity extends Activity {
    private String[] titulos;
    private DrawerLayout NavDrawerLayout;
    private ListView NavList;
    private ArrayList<Item_objct> NavItms;
    private TypedArray NavIcons;
    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    NavigationAdapter NavAdapter;

//Implementacion del ListView
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Drawer Layout
    NavDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    //Lista
    NavList = (ListView) findViewById(R.id.lista);
    //Declaramos el header el caul sera el layout de header.xml
    View header = getLayoutInflater().inflate(R.layout.header, null);
    //Establecemos header
    NavList.addHeaderView(header);
    //Tomamos listado  de imgs desde drawable
    NavIcons = getResources().obtainTypedArray(R.array.navigation_iconos);
    //Tomamos listado  de titulos desde el string-array de los recursos @string/nav_options
    titulos = getResources().getStringArray(R.array.nav_options);
    //Listado de titulos de barra de navegacion
    NavItms = new ArrayList<Item_objct>();
    //Agregamos objetos Item_objct al array
    //Pedidos
    NavItms.add(new Item_objct(titulos[0], NavIcons.getResourceId(0, -1)));
    //Gastos
    NavItms.add(new Item_objct(titulos[1], NavIcons.getResourceId(1, -1)));
    //Productos
    NavItms.add(new Item_objct(titulos[2], NavIcons.getResourceId(2, -1)));
    //Clientes
    NavItms.add(new Item_objct(titulos[3], NavIcons.getResourceId(3, -1)));
    //Ganancias
    NavItms.add(new Item_objct(titulos[4], NavIcons.getResourceId(4, -1)));
    //Capital
    NavItms.add(new Item_objct(titulos[5], NavIcons.getResourceId(5, -1)));
    //Declaramos y seteamos nuestrp adaptador al cual le pasamos el array con los titulos
    NavAdapter= new NavigationAdapter(this,NavItms);
    NavList.setAdapter(NavAdapter);
    //Siempre vamos a mostrar el mismo titulo
    mTitle = mDrawerTitle = getTitle();

    // Declaramos el mDrawerToggle y las imgs a utilizar
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                   /*Host Activity*/
            NavDrawerLayout,        /*DrawerLayout Object*/
            R.drawable.ic_drawer,   /*Icono de navegacion*/
            R.string.app_name,      /* Nombre de aplicacion*/
            R.string.hello_world    /* "Close drawer" description*/
    ) {
        /**
         * Called when a drawer has settled in a completely closed state
         */
        public void onDrawerClosed(View view) {
            Log.e("Cerrado Completo", "!!");
        }

        /**
         * Called when a drawer has settled in a completely open state
         */
        public void onDrawerOpened(View drawerView) {
            Log.e("Apertura Completa", "!!");
        }
    };

    //Establecemos que mDrawerToggle declarado anteriormente sea el DrawerListener
    NavDrawerLayout.setDrawerListener(mDrawerToggle);
    //Establecemos que el ActionBar muestre el boton Home
    getActionBar().setDisplayHomeAsUpEnabled(true);

这是我的主要活动课程,我只是找不到为什么这个

1 个答案:

答案 0 :(得分:0)

我想你忘了添加一些东西:

1.-覆盖以下Activity方法,将DrawerToggle与状态同步:

@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);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

2.-此外,您需要告诉ActionBar,而不是主页图标必须启用。因此,请在OnCreate中调用以下内容:

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

我希望这会对你有所帮助。

如果这对您没有帮助,请检查:http://developer.android.com/training/implementing-navigation/nav-drawer.html这是一个很好的指南

祝你好运