2017-10-06 5 views
0

J'ai implémenté une disposition de tiroir dans laquelle une disposition de tabulation s'ouvre lorsque je clique sur un élément de la vue de navigation. Le tablayout vient bien, mais le problème est quand je clique sur le bouton de retour de la disposition de l'onglet l'application se bloque avec aucune vue de tiroir trouvé avec la gravité gauche. Aussi, j'ai remarqué qu'il y a un peu de décalage lorsque le tiroir de navigation se ferme lorsque la disposition des onglets arrive, le tiroir ne se ferme pas facilement.DrawerLayout NavigationView Aucune vue de tiroir trouvée avec gravité GAUCHE

Ci-dessous mon code java et xml:

public class Navigation extends AppCompatActivity implements 
     ItemFragment.OnHeadlineSelectedListener, 
     SortFragment.OnHeadlineSelectedListener{ 

    private static final String TAG = Navigation.class.getSimpleName(); 
    Toolbar toolbar; 
    FragmentManager mFragmentManager; 
    private TextView mCounter; 
    private ImageView cart; 
    FragmentTransaction mFragmentTransaction; 
    public int id, tabPosition = 0, tabPositionNavigation = 0; 
    private String drinkToSort = "empty"; 
    SharedPreferences preferences; 
    private LocalStore localStore; 
    private String userName, email, location; 
    private String phone; 
    private DrawerLayout mDrawerLayout; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.navigation); 

     localStore = new LocalStore(this); 
     id = localStore.getLoggedInUser().getId(); 
     userName = localStore.getLoggedInUser().getUserName(); 
     email = localStore.getLoggedInUser().getEmail(); 
     phone = localStore.getLoggedInUser().getPhone(); 
     Log.i(TAG,"USERDATA" + "[" + id + userName + email + phone + location + "]"); 

     relativeLayout = (RelativeLayout) findViewById(R.id.main_content); 
     preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     if (toolbar != null) { 
      setSupportActionBar(toolbar); 
     } 

     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerToggle = new ActionBarDrawerToggle(
       this,     /* host Activity */ 
       mDrawerLayout,   /* DrawerLayout object */ 
       toolbar, /* 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) { 
       super.onDrawerClosed(view); 
       getActionBar().setTitle("hello"); 
      } 

      /** Called when a drawer has settled in a completely open state. */ 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       getActionBar().setTitle("hello"); 
      } 
     }; 

     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      this.tabPosition = extras.getInt("tabPosition"); 
      this.tabPositionNavigation = extras.getInt("tabPositionNavigation"); 
      this.drinkToSort = extras.getString("drinkToSort"); 
      Log.d(TAG, tabPosition + tabPositionNavigation + drinkToSort); 
     } 
     Bundle bundle = new Bundle(); 
     bundle.putString("drinkToSort", drinkToSort); 
     LiquorFragment obj = new LiquorFragment(); 
     obj.setArguments(bundle); 
     int width = getResources().getDisplayMetrics().widthPixels/2; 

     mFragmentManager = getSupportFragmentManager(); 
     mFragmentTransaction = mFragmentManager.beginTransaction(); 
     mFragmentTransaction.replace(R.id.frame_container, new NavigationFragment()).commit(); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.details_activity, menu); 
     RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.cart_no).getActionView(); 
     mCounter = (TextView) badgeLayout.findViewById(R.id.counter); 
     Log.d("sameSelectionCount", CartCounter.getCounter()+""); 
     mCounter.setText(""+CartCounter.getCounter()); 
     cart = (ImageView) badgeLayout.findViewById(R.id.cart); 
     cart.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("cart_no","cart_no"); 
       Intent intent = new Intent(getApplicationContext(), ShoppingList.class); 
       startActivity(intent); 
      } 
     }); 
     return true; 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } 
     else { 
      super.onBackPressed(); 

     } 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     // analytics 
     GoogleAnalytics.getInstance(this).reportActivityStart(this); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     //TabFragment.viewPager.setCurrentItem(1); 
     Log.d("landposzq",tabPositionNavigation + ""); 
     if (tabPositionNavigation != 0){ 
      if (tabPositionNavigation == 1){ 
       NavigationFragment.viewPager.setCurrentItem(0); 
      } else if (tabPositionNavigation == 2){ 
       NavigationFragment.viewPager.setCurrentItem(1); 
      } else if (tabPositionNavigation == 3){ 
       NavigationFragment.viewPager.setCurrentItem(2); 
      } else if (tabPositionNavigation == 4){ 
       NavigationFragment.viewPager.setCurrentItem(3); 
      } else if (tabPositionNavigation == 5){ 
       NavigationFragment.viewPager.setCurrentItem(4); 
      } 
     } 

    } 
} 


<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    tools:openDrawer="start" 
    android:fitsSystemWindows="true"> 

    <!--Main content--> 
    <RelativeLayout 
     android:id="@+id/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/qb" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

     <FrameLayout 
      android:id="@+id/frame_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/toolbar" /> 

    </RelativeLayout> 

    <!--Navigation Drawer--> 
    <android.support.design.widget.NavigationView 
     android:id="@+id/main_drawer" 
     android:layout_width="290dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     android:background="@color/white" 
     app:headerLayout="@layout/drawer_header" 
     app:itemTextColor="#000000" 
     app:itemIconTint="@color/qb" 
     app:menu="@menu/menu_drawer" > 

     <LinearLayout 
      android:id="@+id/layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:orientation="vertical"> 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="bottom" 
       android:layout_marginLeft="20dp" 
       android:src="@drawable/balloon4" 
       /> 

      <TextView 
       android:id="@+id/free_drinks" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ffff99" 
       android:gravity="center_vertical" 
       android:padding="10dp" 
       android:drawableRight="@drawable/direction" 
       android:height="60dp" 
       android:textSize="18sp" 
       android:text="GET FREE DRINKS" 
       android:textColor="#000000" 
       android:layout_gravity="bottom" 
       /> 
     </LinearLayout> 
    </android.support.design.widget.NavigationView> 
</android.support.v4.widget.DrawerLayout> 
+0

Utilisez votre variable définie globalement de DrawerLayout dans la méthode onBackPressed() aussi. –

+0

J'ai donné une réponse. S'il vous plaît se référer à cela. –

+0

il échoue toujours, quand j'appuie sur le bouton de retour du téléphone cela fonctionne, mais quand j'appuie sur l'icône retour de la barre d'outils, il se bloque –

Répondre

0

Changez votre méthode onBackPressed() comme ci-dessous:

@Override 
public void onBackPressed() { 

    if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) { 
     mDrawerLayout.closeDrawer(GravityCompat.START); 
    } 
    else { 
     super.onBackPressed(); 

    } 

}