-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BottomSheetMenuDialog as a workaround for support library issues
- Loading branch information
1 parent
5d7b27e
commit d4ec3b4
Showing
7 changed files
with
118 additions
and
8 deletions.
There are no files selected for viewing
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
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
106 changes: 106 additions & 0 deletions
106
library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetMenuDialog.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.github.rubensousa.bottomsheetbuilder; | ||
|
||
import android.content.Context; | ||
import android.support.annotation.NonNull; | ||
import android.support.design.widget.BottomSheetBehavior; | ||
import android.support.design.widget.BottomSheetDialog; | ||
import android.support.design.widget.CoordinatorLayout; | ||
import android.support.v4.view.MotionEventCompat; | ||
import android.util.TypedValue; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
|
||
public class BottomSheetMenuDialog extends BottomSheetDialog { | ||
|
||
private BottomSheetBehavior.BottomSheetCallback mCallback; | ||
private BottomSheetBehavior mBehavior; | ||
|
||
public BottomSheetMenuDialog(Context context) { | ||
super(context); | ||
} | ||
|
||
public BottomSheetMenuDialog(Context context, int theme) { | ||
super(context, theme); | ||
} | ||
|
||
@Override | ||
public void setContentView(View view) { | ||
getDelegate().setContentView(wrapInBottomSheet(view)); | ||
} | ||
|
||
public void setBottomSheetCallback(BottomSheetBehavior.BottomSheetCallback callback) { | ||
mCallback = callback; | ||
} | ||
|
||
public BottomSheetBehavior getBehavior() { | ||
return mBehavior; | ||
} | ||
|
||
private View wrapInBottomSheet(View view) { | ||
final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(), | ||
R.layout.bottomsheetbuilder_dialog, null); | ||
|
||
CoordinatorLayout.LayoutParams layoutParams | ||
= new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, | ||
CoordinatorLayout.LayoutParams.WRAP_CONTENT); | ||
|
||
layoutParams.setBehavior(new BottomSheetBehavior()); | ||
coordinator.addView(view, layoutParams); | ||
mBehavior = BottomSheetBehavior.from(view); | ||
mBehavior.setBottomSheetCallback(mBottomSheetCallback); | ||
view.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); | ||
} | ||
}); | ||
|
||
if (shouldWindowCloseOnTouchOutside()) { | ||
final View finalView = view; | ||
coordinator.setOnTouchListener(new View.OnTouchListener() { | ||
@Override | ||
public boolean onTouch(View v, MotionEvent event) { | ||
if (isShowing() && | ||
MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_UP && | ||
!coordinator.isPointInChildBounds(finalView, | ||
(int) event.getX(), (int) event.getY())) { | ||
cancel(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
}); | ||
} | ||
return coordinator; | ||
} | ||
|
||
private boolean shouldWindowCloseOnTouchOutside() { | ||
TypedValue value = new TypedValue(); | ||
|
||
return getContext().getTheme() | ||
.resolveAttribute(android.R.attr.windowCloseOnTouchOutside, value, true) | ||
&& value.data != 0; | ||
} | ||
|
||
private BottomSheetBehavior.BottomSheetCallback mBottomSheetCallback | ||
= new BottomSheetBehavior.BottomSheetCallback() { | ||
@Override | ||
public void onStateChanged(@NonNull View bottomSheet, | ||
@BottomSheetBehavior.State int newState) { | ||
|
||
if (mCallback != null) { | ||
mCallback.onStateChanged(bottomSheet, newState); | ||
} | ||
if (newState == BottomSheetBehavior.STATE_COLLAPSED) { | ||
dismiss(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onSlide(@NonNull View bottomSheet, float slideOffset) { | ||
if (mCallback != null) { | ||
mCallback.onSlide(bottomSheet, slideOffset); | ||
} | ||
} | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:soundEffectsEnabled="false" /> |
File renamed without changes.
File renamed without changes.
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