Skip to content

Commit

Permalink
Add BottomSheetMenuDialog as a workaround for support library issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensousa committed Mar 1, 2016
1 parent 5d7b27e commit d4ec3b4
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
Expand All @@ -11,7 +10,6 @@
import android.support.annotation.MenuRes;
import android.support.annotation.StyleRes;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.view.SupportMenuInflater;
Expand Down Expand Up @@ -162,9 +160,9 @@ public View createView() {
return sheet;
}

public BottomSheetDialog createDialog() {
BottomSheetDialog dialog = mTheme == 0 ? new BottomSheetDialog(mContext)
: new BottomSheetDialog(mContext, mTheme);
public BottomSheetMenuDialog createDialog() {
BottomSheetMenuDialog dialog = mTheme == 0 ? new BottomSheetMenuDialog(mContext)
: new BottomSheetMenuDialog(mContext, mTheme);

View sheet = setupView();
sheet.findViewById(R.id.fakeShadow).setVisibility(View.GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

if (viewType == TYPE_HEADER) {
return new HeaderViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.bottomsheetbuilder_list_header_adapter, parent, false));
.inflate(R.layout.bottomsheetbuilder_list_header, parent, false));
}

if (viewType == TYPE_ITEM) {
Expand All @@ -87,7 +87,7 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

if (viewType == TYPE_DIVIDER) {
return new DividerViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.bottomsheetbuilder_list_divider_adapter, parent, false));
.inflate(R.layout.bottomsheetbuilder_list_divider, parent, false));
}

}
Expand Down
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);
}
}
};
}
5 changes: 5 additions & 0 deletions library/src/main/res/layout/bottomsheetbuilder_dialog.xml
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" />
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import com.github.rubensousa.bottomsheetbuilder.BottomSheetBuilder;
import com.github.rubensousa.bottomsheetbuilder.BottomSheetItemClickListener;
import com.github.rubensousa.bottomsheetbuilder.BottomSheetMenuDialog;
import com.github.rubensousa.bottomsheetbuilder.items.BottomSheetMenuItem;

public class MainActivity extends AppCompatActivity
Expand Down Expand Up @@ -85,7 +86,7 @@ public void onBottomSheetItemClick(BottomSheetMenuItem item) {
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}

private BottomSheetDialog createDialog(@MenuRes int menu) {
private BottomSheetMenuDialog createDialog(@MenuRes int menu) {
return new BottomSheetBuilder(this, R.style.AppTheme_BottomSheetDialog)
.setMode(BottomSheetBuilder.MODE_LIST)
.setBackgroundColor(android.R.color.white)
Expand Down

0 comments on commit d4ec3b4

Please sign in to comment.