Skip to content
This repository was archived by the owner on Jan 22, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ boolean onTouchEvent(MotionEvent ev) {
final int slop = dragger.getTouchSlop();
if (dx * dx + dy * dy < slop * slop) {
// Taps close a dimmed open drawer but only if it isn't locked open.
if ((openState & FLAG_IS_OPENED) == FLAG_IS_OPENED) { // TODO isDrawerOpen method?
if (isDrawerOpen()) { // TODO isDrawerOpen method?
peekingOnly = false;
}
}
Expand Down Expand Up @@ -276,7 +276,7 @@ private void updateDrawerState(int activeState, View activeDrawer) {
}

private void dispatchOnDrawerClosed(View drawerView) {
if ((openState & FLAG_IS_OPENED) == FLAG_IS_OPENED) {
if (isDrawerOpen()) {
openState = 0;

updateChildrenImportantForAccessibility(drawerView, false);
Expand All @@ -294,7 +294,7 @@ private void dispatchOnDrawerClosed(View drawerView) {
}

private void dispatchOnDrawerOpened(View drawerView) {
if ((openState & FLAG_IS_OPENED) == 0) {
if (!isDrawerOpen()) {
openState = FLAG_IS_OPENED;

updateChildrenImportantForAccessibility(drawerView, true);
Expand Down Expand Up @@ -325,8 +325,7 @@ private void updateChildrenImportantForAccessibility(View drawerView, boolean is
}
}

@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
@Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
int childWidth = changedView.getWidth();

// This reverses the positioning shown in onLayout.
Expand Down Expand Up @@ -393,6 +392,55 @@ private void setDrawerViewOffset(float slideOffset) {
return child.getTop();
}

public boolean isDrawerOpen() {
return (openState & FLAG_IS_OPENED) == FLAG_IS_OPENED;
}

public boolean isDrawerOpening(){
return (openState & FLAG_IS_OPENING) == FLAG_IS_OPENING;
}

public boolean isDrawerClosing(){
return (openState & FLAG_IS_CLOSING) == FLAG_IS_CLOSING;
}

public void openDrawer() {
if (isDrawerOpen() || isDrawerOpening()) {
return;
}

boolean needsSettle;

if (isLeft) {
needsSettle = dragger.smoothSlideViewTo(child, 0, child.getTop());
} else {
needsSettle =
dragger.smoothSlideViewTo(child, parent.getWidth() - child.getWidth(), child.getTop());
}

if (needsSettle) {
ViewCompat.postOnAnimation(parent, draggerSettle);
}
}

public void closeDrawer() {
if (!isDrawerOpen() || isDrawerClosing()) {
return;
}

boolean needsSettle;

if (isLeft) {
needsSettle = dragger.smoothSlideViewTo(child, -child.getWidth(), child.getTop());
} else {
needsSettle = dragger.smoothSlideViewTo(child, parent.getWidth(), child.getTop());
}

if (needsSettle) {
ViewCompat.postOnAnimation(parent, draggerSettle);
}
}

boolean onLayoutChild() {
int width = parent.getMeasuredWidth();
int height = parent.getMeasuredHeight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import android.content.res.TypedArray;
import android.support.annotation.Keep;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.util.SimpleArrayMap;
import android.support.v4.util.ArrayMap;
import android.support.v4.view.GravityCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

public final class DrawerBehavior extends CoordinatorLayout.Behavior<View> {
private static void validateGravity(int gravity) {
Expand All @@ -36,7 +37,7 @@ private static void validateGravity(int gravity) {
}
}

private final SimpleArrayMap<View, BehaviorDelegate> delegates = new SimpleArrayMap<>();
private final ArrayMap<View, BehaviorDelegate> delegates = new ArrayMap<>();
private final int gravity;

@SuppressWarnings("unused") // Public API for programmatic instantiation.
Expand Down Expand Up @@ -66,18 +67,40 @@ private BehaviorDelegate delegate(CoordinatorLayout parent, View child) {
return delegate;
}

@Override
public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
@Override public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
return child.getVisibility() == View.GONE //
|| delegate(parent, child).onLayoutChild();
}

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {
@Override public boolean onInterceptTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {
return delegate(parent, child).onInterceptTouchEvent(ev);
}

@Override public boolean onTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {
return delegate(parent, child).onTouchEvent(ev);
}

public void open(){
for(BehaviorDelegate delegate : delegates.values()){
delegate.openDrawer();
}
}

public void close(){
for(BehaviorDelegate delegate : delegates.values()){
delegate.closeDrawer();
}
}

public static DrawerBehavior from(View view) {
ViewGroup.LayoutParams params = view.getLayoutParams();
if (!(params instanceof CoordinatorLayout.LayoutParams)) {
throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
}
CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) params).getBehavior();
if (!(behavior instanceof DrawerBehavior)) {
throw new IllegalArgumentException("The view is not associated with DrawerBehavior");
}
return (DrawerBehavior) behavior;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.jakewharton.behavior.drawer.DrawerBehavior;

public final class DrawerBehaviorActivity extends AppCompatActivity {

private DrawerBehavior drawerBehavior;

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_behavior);
drawerBehavior = DrawerBehavior.from(findViewById(R.id.drawerView));

//noinspection ConstantConditions
findViewById(R.id.openButton).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
drawerBehavior.open();
}
});

//noinspection ConstantConditions
findViewById(R.id.closeButton).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
drawerBehavior.close();
}
});
}
}
20 changes: 19 additions & 1 deletion sample/src/main/res/layout/drawer_behavior.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,29 @@
android:layout_height="match_parent"
android:background="#feee"
>
<View
<FrameLayout
android:id="@+id/drawerView"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ff00"
app:layout_behavior="com.jakewharton.behavior.drawer.DrawerBehavior"
>
<Button
android:id="@+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:text="Close drawer"
/>

</FrameLayout>

<Button
android:id="@+id/openButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Open drawer"
/>
</android.support.design.widget.CoordinatorLayout>