Je travaille sur un projet Android et j'essaie d'intégrer le nouveau tiroir de navigation en utilisant l'exemple de http://developer.Android.com/training/implementing-navigation /nav-drawer.html .
Cela fonctionne principalement, sauf pour une chose, le bouton vers le haut de la barre d'action n'affiche pas le menu, mais si je glisse mon doigt sur l'écran d'activité principal depuis l'Edge, le menu apparaît, donc je sais qu'il n'y a rien de mal avec le menu réel , c'est juste le bouton de la barre d'action.
Voici le code
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle;
private CharSequence mDrawerTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList = (ListView)findViewById(R.id.left_drawer);
MenuItemAdapter menuAdapter = new MenuItemAdapter(this);
menuAdapter.add(new MenuItem("Hello"));
menuAdapter.add(new MenuItem("World"));
menuAdapter.add(new MenuItem("Parsnips"));
menuAdapter.add(new MenuItem("Turnips"));
mDrawerList.setAdapter(menuAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_closed)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null)
{
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
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) {
// update the main content by replacing fragments
Android.app.Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARGS_PLANET_NUMBER, position);
fragment.setArguments(args);
Android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title)
{
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
public static class PlanetFragment extends Android.app.Fragment
{
public static final String ARGS_PLANET_NUMBER = "planet_number";
public PlanetFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflator.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARGS_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
//((TextView)rootView.findViewById(R.id.fragment_text_view)).setText(planet);
getActivity().setTitle("Planet: " + planet);
return rootView;
}
}
private class MenuItem
{
public String menuName;
public MenuItem(String menuName)
{
this.menuName = menuName;
}
}
public class MenuItemAdapter extends ArrayAdapter<MenuItem>
{
public MenuItemAdapter (Context context)
{
super(context,0);
}
public View getView (int position, View convertView, ViewGroup parent)
{
convertView = null;
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_menu_item, null);
}
/*TextView title = (TextView)convertView.findViewById(R.id.row_title);
title.setText(getItem(position).menuName);
title.setVisibility(View.VISIBLE);*/
EditText title = (EditText)convertView.findViewById(R.id.row_edittext);
title.setHint(getItem(position).menuName);
title.setVisibility(View.VISIBLE);
return convertView;
}
}
}
Merci pour toute l'aide que vous pouvez apporter.
Vous avez oublié d'implémenter onOptionsItemSelected
C'est là que la magie opère:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
Pour ceux qui ont encore des problèmes, il se peut que vous manquiez cette méthode (que OP a mais que j'avais supprimée):
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
Son travail pour moi.
drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDrawer.openDrawer(GravityCompat.START);
}
});
Ici, je vais vous dire la manière SIMPLE ET FACILE de créer une navigation dans les tiroirs dans Android sans utiliser de Android studio. Je viens d'utiliser un ADT pour créer un tiroir de navigation) Voici le code jetez un oeil
activity_main.xml
<Android.support.v4.widget.DrawerLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:id="@+id/dr_layout"
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<RelativeLayout
Android:id="@+id/mainContent"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content">
<TextView
Android:id="@+id/txt1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="swipe content"/>
<Button
Android:id="@+id/bt1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text = "Click to open d"/>
</RelativeLayout>
<RelativeLayout
Android:id="@+id/drawer"
Android:layout_width="200dp"
Android:layout_height="match_parent"
Android:layout_gravity = "start"
Android:background="#FFFFFF">
<TextView
Android:id="@+id/txt2"
Android:layout_width="200dp"
Android:layout_height="wrap_content"
Android:text="drawer content are here arr"/>
<Button
Android:id="@+id/bt2"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text = "Click to open d"/>
</RelativeLayout>
</Android.support.v4.widget.DrawerLayout>
MainActivity.Java
package com.example.drawer1;
import Android.os.Bundle;
import Android.annotation.SuppressLint;
import Android.app.Activity;
import Android.content.res.Configuration;
import Android.support.v4.app.ActionBarDrawerToggle;
import Android.support.v4.widget.DrawerLayout;
import Android.support.v4.widget.DrawerLayout.DrawerListener;
import Android.util.Log;
import Android.view.Menu;
import Android.view.MenuItem;
import Android.view.MotionEvent;
import Android.view.View;
import Android.view.View.OnClickListener;
import Android.view.View.OnTouchListener;
import Android.widget.Button;
import Android.widget.Toast;
public class MainActivity extends Activity {
private DrawerLayout drawerLayout;
private View drawerView;
Button bt1,bt2;
private DrawerListener myDrawerListner;
private ActionBarDrawerToggle mDrawerTogle;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.dr_layout);
drawerView = (View) findViewById(R.id.drawer);
drawerLayout.setDrawerListener(myDrawerListner);
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
mDrawerTogle =new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer,R.string.open_drawer,R.string.close_drawer){
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
super.onDrawerOpened(drawerView);
getActionBar().setTitle("SpeakEng");
}
public void onDrawerClosed(View view) {
// TODO Auto-generated method stub
super.onDrawerClosed(view);
getActionBar().setTitle("SpeakEng");
}
};
drawerLayout.setDrawerListener(mDrawerTogle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
drawerLayout.closeDrawer(drawerView);
}
});
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
drawerLayout.openDrawer(drawerView);
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
mDrawerTogle.onConfigurationChanged(newConfig);
}
public boolean onOptionsItemSelected(MenuItem item){
if (mDrawerTogle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
//Sync the toogle state after onRestoreInstanceState has occured.
mDrawerTogle.syncState();
}
}
Vous pouvez ouvrir le tiroir 1) en cliquant sur le bouton, 2) en tirant de la gauche, 3) en cliquant sur l'icône du tiroir dans la barre d'action.Comme vous le souhaitez, vous pouvez l'ouvrir.Je vous ai donné trois options.
note: tiroirLayout doit être l'élément racine comme indiqué dans le code. et conservez onConfigurationChanged (), onOptionsItemSelected (), onPostCreate (). ces trois méthodes sont très importantes pour créer un tiroir de navigation.
Bonne chance!.