Add end drawer to the sample app.

Bug: 26555813
Change-Id: Ia7f00f8bb1a3342750c5c2db76b6ea1bb7cbb1bd
This commit is contained in:
Kirill Grouchnikov
2016-01-14 10:50:59 -05:00
parent 39eac83a81
commit 7da5bbb6e9
11 changed files with 49 additions and 15 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 B

View File

@@ -76,7 +76,21 @@
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ff333333"
android:background="#333333"
android:fitsSystemWindows="true"/>
<!-- android:layout_gravity="end" tells DrawerLayout to treat
this as a sliding drawer on the ending side, which is
right for left-to-right locales. The drawer is given arbitrary
initial width and extends the full height of the container. A
solid background is used for contrast with the content view.
The actual width of drawer will be determined at runtime based on
the screen size according to the Material spec. -->
<FrameLayout
android:id="@+id/end_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#808080"/>
</android.support.v4.widget.DrawerLayout>

View File

@@ -27,10 +27,10 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.example.android.supportv7.R;
@@ -77,7 +77,8 @@ import com.example.android.supportv7.Shakespeare;
*/
public class DrawerLayoutActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawer;
private ListView mStartDrawer;
private FrameLayout mEndDrawer;
private TextView mContent;
private ActionBarDrawerToggle mDrawerToggle;
@@ -90,19 +91,21 @@ public class DrawerLayoutActivity extends AppCompatActivity {
setContentView(R.layout.drawer_layout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawer = (ListView) findViewById(R.id.start_drawer);
mStartDrawer = (ListView) findViewById(R.id.start_drawer);
mEndDrawer = (FrameLayout) findViewById(R.id.end_drawer);
mContent = (TextView) findViewById(R.id.content_text);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow_end, GravityCompat.END);
// The drawer title must be set in order to announce state changes when
// accessibility is turned on. This is typically a simple description,
// e.g. "Navigation".
mDrawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.drawer_title));
mDrawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
mStartDrawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
Shakespeare.TITLES));
mDrawer.setOnItemClickListener(new DrawerItemClickListener());
mStartDrawer.setOnItemClickListener(new DrawerItemClickListener());
// Find the toolbar in our layout and set it as the support action bar on the activity.
// This is required to have the drawer slide "over" the toolbar.
@@ -171,10 +174,15 @@ public class DrawerLayoutActivity extends AppCompatActivity {
final int maxDrawerWidth = Math.max(0, drawerLayoutWidth - actionBarSize);
final int drawerWidth = Math.min(idealDrawerWidth, maxDrawerWidth);
final DrawerLayout.LayoutParams drawerLp =
(DrawerLayout.LayoutParams) mDrawer.getLayoutParams();
drawerLp.width = drawerWidth;
mDrawer.setLayoutParams(drawerLp);
final DrawerLayout.LayoutParams startDrawerLp =
(DrawerLayout.LayoutParams) mStartDrawer.getLayoutParams();
startDrawerLp.width = drawerWidth;
mStartDrawer.setLayoutParams(startDrawerLp);
final DrawerLayout.LayoutParams endDrawerLp =
(DrawerLayout.LayoutParams) mEndDrawer.getLayoutParams();
endDrawerLp.width = drawerWidth;
mEndDrawer.setLayoutParams(endDrawerLp);
// Remove ourselves as the pre-draw listener since this is a one-time
// configuration.
@@ -206,10 +214,22 @@ public class DrawerLayoutActivity extends AppCompatActivity {
@Override
public void onBackPressed() {
// Is the drawer open?
if (mDrawerLayout.isDrawerOpen(mDrawer)) {
// Close the drawer and return.
mDrawerLayout.closeDrawer(mDrawer);
boolean hadOpenDrawer = false;
// Is the start drawer open?
if (mDrawerLayout.isDrawerOpen(mStartDrawer)) {
// Close it
mDrawerLayout.closeDrawer(mStartDrawer);
hadOpenDrawer = true;
}
// Is the end drawer open?
if (mDrawerLayout.isDrawerOpen(mEndDrawer)) {
// Close it
mDrawerLayout.closeDrawer(mEndDrawer);
hadOpenDrawer = true;
}
if (hadOpenDrawer) {
// If we had one or both drawers open, now that we've closed it / them, return.
return;
}
@@ -231,7 +251,7 @@ public class DrawerLayoutActivity extends AppCompatActivity {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mContent.setText(Shakespeare.DIALOGUE[position]);
mToolbar.setTitle(Shakespeare.TITLES[position]);
mDrawerLayout.closeDrawer(mDrawer);
mDrawerLayout.closeDrawer(mStartDrawer);
}
}
}