Reputation: 2531
I have two drawers on left and on right. Now right drawer is in the lock-closed state, but left drawer can be open and close. On opening drawer, I am pushing the middle content using drawer offset. Now I also want right drawer (which is opened by default) should move/adjust the content also but I am unable to do it. Because when the left drawer is opened it pushes the content under the right drawer. So what I want the right drawer on default opening should adjust the content and when I open left drawer content should adjust and should not move under the right drawer.
Here is my code:
<DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<RelativeLayout
android:id="@+id/main_screen"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/bg">
<include
android:id="@+id/includedView"
layout="@layout/view" />
</RelativeLayout>
<!-- ======= side menu ====== -->
<RelativeLayout
android:id="@+id/leftMenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left">
<include
layout="@layout/leftmenuLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rightMenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right">
<include
layout="@layout/rightmenuLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</RelativeLayout>
<!-- ======================= -->
</DrawerLayout>
DrawerAcivity:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setScrimColor(Color.parseColor("#00000000"));
main_screen = (RelativeLayout) findViewById(R.id.main_screen);
leftMenu= (RelativeLayout) findViewById(R.id.leftMenu);
rightMenu= (RelativeLayout)
findViewById(R.id.rightMenu);
Display display = getWindowManager().getDefaultDisplay();
size = new Point();
display.getSize(size);
mDrawerLayout.addDrawerListener(new CustomDrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
float moveFactor = 0;
moveFactor = (drawerView.getWidth() * slideOffset);
main_screen.setTranslationX(moveFactor);
}
@Override
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
invalidateOptionsMenu();
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, rightMenu);
mDrawerLayout.openDrawer(GravityCompat.END);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, leftMenu);
ViewGroup.LayoutParams params = leftMenu.getLayoutParams();
params.width = (((size.x / 2) / 4));
rl_slider_menu.setLayoutParams(params);
ViewGroup.LayoutParams paramsRight =
rightMenu.getLayoutParams();
paramsRight.width = ((size.x / 3));
rightMenu.setLayoutParams(paramsRight);
DrawerLayout.LayoutParams paramsContent = (DrawerLayout.LayoutParams) main_screen.getLayoutParams();
paramsContent.setMargins(0, 0, (((size.x / 2) / 4)), 0); //substitute parameters for left, top, right, bottom
main_screen.setLayoutParams(paramsContent);
main_screen.invalidate();
Upvotes: 2
Views: 416
Reputation: 39201
This actually turns out to be a pretty simple implementation, once you get your head wrapped around it.
We simply need to keep track of both drawers' inset measures, scale the content View
to the remaining space, and then translate it by the difference of half of each inset. For example. building on the code in the question:
mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
private float leftInset, rightInset;
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
if (drawerView == leftMenu) {
leftInset = slideOffset * leftMenu.getWidth();
}
else {
rightInset = slideOffset * rightMenu.getWidth();
}
final float scale = 1 - (leftInset + rightInset) / main_screen.getWidth();
main_screen.setScaleX(scale);
final float translation = leftInset / 2 - rightInset / 2;
main_screen.setTranslationX(translation);
}
...
}
Upvotes: 2