2013-06-13 4 views
1

J'ai lu la documentation de NavigationDrawer et j'ai essayé de la créer dans ma propre application. Mais il y a un problème: le ListView qui doit être utilisé en tant que menu flotte juste au-dessus du contenu principal et je ne peux pas effectuer d'actions avec celui-ci (par exemple fermer par balayage). Quel est le problème?NavigationDrawer ne fonctionne pas

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
    android:id="@+id/mainPageLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#e5e5e5" 
    android:orientation="vertical" > 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 

     <android.support.v4.view.ViewPager 
      android:id="@+id/mainScreenViewPager" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" > 

      <android.support.v4.view.PagerTitleStrip 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="top" /> 
     </android.support.v4.view.ViewPager> 
    </FrameLayout> 
</LinearLayout> 

<ListView 
    android:id="@+id/navigationDrawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" 
    android:gravity="start" > 
</ListView> 

</android.support.v4.widget.DrawerLayout> 

Et l'initialisation du tiroir:

String[] titles = getResources().getStringArray(R.array.lists_titles); 
    ListView drawer = (ListView) findViewById(R.id.navigationDrawer); 
    drawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles)); 
    drawer.setOnItemClickListener(new DrawerItemClickListener()); 

Merci.

+0

Avez-vous essayé de mettre la navigationDrawer ListView à l'intérieur du LinearLayout après la FrameLayout? – Squonk

+0

Ce 'FrameLayout' est plutôt étrange en premier lieu. Je ne sais pas pourquoi c'est là. Tout ce qu'il fait est de rendre votre application plus lente sans valeur évidente. – CommonsWare

Répondre

2

Voici un échantillon approximatif de codes qui fonctionnent pour moi, ils sont une combinaison de choses android developers site et StylingAndroid blog:

DrawerLayout mDrawerLayout; 
ActionBarDrawerToggle mDrawerToggle; 

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.your_layoutxml_id); 

    // must initialize mDrawerLayout and mDrawerToggle in main thread 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

    mDrawerToggle = new ActionBarDrawerToggle(
      this,     /* host Activity */ 
      mDrawerLayout,   /* DrawerLayout object */ 
      R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */ 
      R.string.drawer_open, /* "open drawer" description */ 
      R.string.drawer_close /* "close drawer" description */ 
      ) { 

     /** Called when a drawer has settled in a completely closed state. */ 
     public void onDrawerClosed(View view) { 
      // do something 
     } 

     /** Called when a drawer has settled in a completely open state. */ 
     public void onDrawerOpened(View drawerView) { 
      // do something 
     } 
    }; 

    String[] titles = getResources().getStringArray(R.array.lists_titles); 
    ListView drawer = (ListView) findViewById(R.id.navigationDrawer); 
    drawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles)); 
    drawer.setOnItemClickListener(new DrawerItemClickListener()); 

    // Set the drawer toggle as the DrawerListener 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 
    ActionBar actionbar = getSupportActionBar(); 
    actionbar.setDisplayHomeAsUpEnabled(true); 
    actionbar.setHomeButtonEnabled(true); 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) 
{ 
    super.onPostCreate(savedInstanceState); 
    mDrawerToggle.syncState(); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    mDrawerToggle.onConfigurationChanged(newConfig); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection 
    if(item.getItemId() == android.R.id.home) { 
     if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) { 
      mDrawerLayout.closeDrawer(GravityCompat.START); 
     } else { 
      mDrawerLayout.openDrawer(GravityCompat.START); 
     } 
    } 
    return super.onOptionsItemSelected(item); 
} 
Questions connexes