Created
February 5, 2015 20:56
-
-
Save jc4p/0a1dce9a2006918f75a6 to your computer and use it in GitHub Desktop.
Double DrawerLayout bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:id="@+id/drawer_layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<android.support.v7.widget.Toolbar | |
android:id="@+id/main_toolbar" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="?attr/colorPrimary" | |
android:minHeight="?attr/actionBarSize" | |
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" | |
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> | |
<TextView | |
android:id="@+id/status" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:padding="10dp" /> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerview" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> | |
<LinearLayout | |
android:id="@+id/left_drawer" | |
android:layout_width="240dp" | |
android:layout_height="match_parent" | |
android:layout_gravity="start" | |
android:background="#ACFF0000" | |
android:gravity="center" | |
android:visibility="gone"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="LEFT DRAWER" | |
android:textSize="24sp" /> | |
</LinearLayout> | |
<LinearLayout | |
android:id="@+id/right_drawer" | |
android:layout_width="240dp" | |
android:layout_height="match_parent" | |
android:layout_gravity="end" | |
android:background="#AC00FF00" | |
android:gravity="center" | |
android:visibility="gone"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="RIGHT DRAWER" | |
android:textSize="24sp" /> | |
</LinearLayout> | |
</android.support.v4.widget.DrawerLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kasra.androidsandbox; | |
import android.os.Bundle; | |
import android.support.v4.view.GravityCompat; | |
import android.support.v4.widget.DrawerLayout; | |
import android.support.v7.app.ActionBarActivity; | |
import android.support.v7.app.ActionBarDrawerToggle; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.support.v7.widget.Toolbar; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.squareup.picasso.Picasso; | |
public class MainActivity extends ActionBarActivity { | |
private DrawerLayout mDrawerLayout; | |
private ActionBarDrawerToggle mDrawerToggle; | |
private TextView mStatusTextView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar)findViewById(R.id.main_toolbar); | |
setSupportActionBar(toolbar); | |
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerview); | |
recyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
recyclerView.setHasFixedSize(false); | |
recyclerView.setAdapter(new MyAdapter()); | |
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); | |
mDrawerToggle = new ActionBarDrawerToggle( | |
this, /* host Activity */ | |
mDrawerLayout, /* DrawerLayout object */ | |
R.string.open_drawer, /* "open drawer" description for accessibility */ | |
R.string.close_drawer /* "close drawer" description for accessibility */ | |
); | |
mDrawerLayout.setDrawerListener(mDrawerListener); | |
mStatusTextView = (TextView) findViewById(R.id.status); | |
updateStatus(); | |
} | |
private void updateStatus() { | |
mStatusTextView.setText("Left drawer is: " + (mDrawerLayout.isDrawerOpen(GravityCompat.START) ? "" : "not") + " open and " + (mDrawerLayout.isDrawerVisible(GravityCompat.START) ? "" : "not ") + "visible"); | |
mStatusTextView.append("\nRight drawer is: " + (mDrawerLayout.isDrawerOpen(GravityCompat.END) ? "" : "not") + " open and " + (mDrawerLayout.isDrawerVisible(GravityCompat.END) ? "" : "not ") + "visible") ; | |
} | |
private class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { | |
protected class ViewHolder extends RecyclerView.ViewHolder { | |
protected TextView headerView; | |
protected ImageView imageView; | |
public ViewHolder(View itemView) { | |
super(itemView); | |
headerView = (TextView)itemView.findViewById(R.id.list_item_header); | |
imageView = (ImageView)itemView.findViewById(R.id.list_item_image); | |
} | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); | |
return new ViewHolder(v); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder viewHolder, int position) { | |
viewHolder.headerView.setText("Cat #" + position); | |
String url = "http://lorempixel.com/400/400/cats/" + "?rand=" + (position * position); | |
Picasso.with(MainActivity.this).load(url).into(viewHolder.imageView); | |
} | |
@Override | |
public int getItemCount() { | |
return 10; | |
} | |
} | |
private DrawerLayout.DrawerListener mDrawerListener = new DrawerLayout.DrawerListener() { | |
@Override | |
public void onDrawerSlide(View drawerView, float slideOffset) { | |
mDrawerToggle.onDrawerSlide(drawerView, slideOffset); | |
updateStatus(); | |
} | |
@Override | |
public void onDrawerOpened(View drawerView) { | |
mDrawerToggle.onDrawerOpened(drawerView); | |
updateStatus(); | |
} | |
@Override | |
public void onDrawerClosed(View drawerView) { | |
mDrawerToggle.onDrawerClosed(drawerView); | |
updateStatus(); | |
} | |
@Override | |
public void onDrawerStateChanged(int newState) { | |
mDrawerToggle.onDrawerStateChanged(newState); | |
updateStatus(); | |
} | |
}; | |
} |
This is fixed if you remove android:visibility
from the drawers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's what happens the first time you open the left drawer after a cold start: http://cl.ly/3G0C2O0d0U0G