diff --git a/library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetBuilder.java b/library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetBuilder.java index 846c1e9..7c6e12a 100644 --- a/library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetBuilder.java +++ b/library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetBuilder.java @@ -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; @@ -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; @@ -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); diff --git a/library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetItemAdapter.java b/library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetItemAdapter.java index b1fbf0d..d949b1e 100644 --- a/library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetItemAdapter.java +++ b/library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetItemAdapter.java @@ -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) { @@ -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)); } } diff --git a/library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetMenuDialog.java b/library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetMenuDialog.java new file mode 100644 index 0000000..ac9be1d --- /dev/null +++ b/library/src/main/java/com/github/rubensousa/bottomsheetbuilder/BottomSheetMenuDialog.java @@ -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); + } + } + }; +} diff --git a/library/src/main/res/layout/bottomsheetbuilder_dialog.xml b/library/src/main/res/layout/bottomsheetbuilder_dialog.xml new file mode 100644 index 0000000..e1af061 --- /dev/null +++ b/library/src/main/res/layout/bottomsheetbuilder_dialog.xml @@ -0,0 +1,5 @@ + + diff --git a/library/src/main/res/layout/bottomsheetbuilder_list_divider_adapter.xml b/library/src/main/res/layout/bottomsheetbuilder_list_divider.xml similarity index 100% rename from library/src/main/res/layout/bottomsheetbuilder_list_divider_adapter.xml rename to library/src/main/res/layout/bottomsheetbuilder_list_divider.xml diff --git a/library/src/main/res/layout/bottomsheetbuilder_list_header_adapter.xml b/library/src/main/res/layout/bottomsheetbuilder_list_header.xml similarity index 100% rename from library/src/main/res/layout/bottomsheetbuilder_list_header_adapter.xml rename to library/src/main/res/layout/bottomsheetbuilder_list_header.xml diff --git a/sample/src/main/java/com/github/rubensousa/bottomsheetbuilder/sample/MainActivity.java b/sample/src/main/java/com/github/rubensousa/bottomsheetbuilder/sample/MainActivity.java index 968230f..689bdec 100644 --- a/sample/src/main/java/com/github/rubensousa/bottomsheetbuilder/sample/MainActivity.java +++ b/sample/src/main/java/com/github/rubensousa/bottomsheetbuilder/sample/MainActivity.java @@ -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 @@ -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)