Keep Buttomsheet in collapsible state - android

I have a bottom sheet that has inside a listview, I don't want to have the behaviour to expand or to dismiss, I just want to keep it at the fixed size, Now it shows at the size I want (70% of screen) but on slide up or down it changes its state.
Is there a way i can do this?
I have tried:
BottomSheetBehavior.BottomSheetCallback bottomSheetCallback = new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
// dismiss();
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
};
// bottomSheetBehavior.setBottomSheetCallback(bottomSheetCallback);
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(bottomSheetCallback);
((BottomSheetBehavior) behavior).setPeekHeight(((RestaurantsActivity) getActivity()).getHeightForFragment());
}

Related

Raw use of parameterized class on Android Studio

I have this code on my Android project:
final BottomSheetBehavior infoBottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.info_view));
infoBottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
infoBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
}
});
And the next warning appears over BottomSheetBehavior: Raw use of parameterized class 'BottomSheetBehavior'
Does anybody know how to avoid this warning?
Ok, here is the so basic answer from a coffee talk...
final BottomSheetBehavior<View> infoBottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.info_view));
infoBottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
infoBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
}
});
I've just missed the <View>

Manage Visibility of button in Horizontal Recycler list view

I have a horizontal center zoom image list in which i want to display play icon and text for center scale view only
I am able to scale center Imageview but getting position problem while managing it through model.
But sometimes it displays and sometimes not. but i am getting position of center imageview perfectly in class onScrollStateChanged method.
Following class i have used for layout manager and scrollview in my activity
mAdapter = new ContactScreenAdapter(ContactScreenActivity.this, mContext, listFavoriteContact);
scrollZoomLayoutManager = new ScrollZoomLayoutManager(this, Dp2px(25));
recyclerFavoriteList.setLayoutManager(scrollZoomLayoutManager);
recyclerFavoriteList.setAdapter(mAdapter);
recyclerFavoriteList.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (!(layoutManager instanceof CircleLayoutManager) && !(layoutManager instanceof ScrollZoomLayoutManager)) {
mAutoSet = true;
return;
}
if (!mAutoSet) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
final int dx;
if (layoutManager instanceof CircleLayoutManager) {
dx = ((CircleLayoutManager) layoutManager).getOffsetCenterView();
} else {
dx = ((ScrollZoomLayoutManager) layoutManager).getOffsetCenterView();
}
recyclerView.smoothScrollBy(dx, 0);
Log.d(TAG,"#### dx : "+ ((ScrollZoomLayoutManager) layoutManager).getCurrentPosition());
mAdapter.setSelecteditem(((ScrollZoomLayoutManager) layoutManager).getCurrentPosition());
}
mAutoSet = true;
}
if (newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING) {
mAutoSet = false;
}
}
});
and i am updating my model in adapter method
public void setSelecteditem(int selecteditem) {
this.selectedItem = selecteditem;
for (int i=0;i<moviesList.size();i++){
moviesList.get(i).setCenterSelected(false);
Log.d("CSA","## moviesList.get(i).isCenterSelected() : "+ moviesList.get(i).isCenterSelected());
}
moviesList.get(selecteditem).setCenterSelected(true);
notifyDataSetChanged();
}
Thank you for help in advance.

BottomSheetDialog disable dragging

I have Bottomsheetdialog with Custom view having EditText and on EditText scrolling bottom sheet goes down, How to disable BottomSheetDialog dragging. Any Help Appreciated.
I might be late, but it works fine for me:
#Override
public void setupDialog(final Dialog dialog, int style) {
super.setupDialog(dialog, style);
// This should do the job
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(true);
}
Simple solution, It solved two problem I had.
1- It block the dragging event.
2- It solves the listView scrolling problem
CoordinatorLayout.Behavior<View> behavior;
View profile_config_layout_bottom_sheet = findViewById(R.id.list_view_audience_profile_config_layout);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) profile_config_layout_bottom_sheet.getLayoutParams();
behavior = layoutParams.getBehavior();
assert behavior != null;
((BottomSheetBehavior<View>) behavior).addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
((BottomSheetBehavior<View>) behavior).setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {}
});
Show the dialog with:
((BottomSheetBehavior) behavior).setState(BottomSheetBehavior.STATE_EXPANDED);

Android BottomSheetBehavior, how to disable snap?

Standard android BottomSheetBehavior has tree state: hidden, collapsed and expanded.
I want to allow user to "leave" bottom sheet between collapsed and expanded. Now, with the default behavior, it will snap to collapsed or expanded based which is closest. How should I disable this snap functionality?
I will present a way to achievie such functionality for a View extending BottomSheetDialogFragment.
Expanding:
First of all overrive onResume:
#Override
public void onResume() {
super.onResume();
addGlobaLayoutListener(getView());
}
private void addGlobaLayoutListener(final View view) {
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
setPeekHeight(v.getMeasuredHeight());
v.removeOnLayoutChangeListener(this);
}
});
}
public void setPeekHeight(int peekHeight) {
BottomSheetBehavior behavior = getBottomSheetBehaviour();
if (behavior == null) {
return;
}
behavior.setPeekHeight(peekHeight);
}
What the code above is supposed to do is just setting the BottomSheet peekHeight to the heigth of the view. The key here is the function getBottomSheetBehaviour(). The implementation is below:
private BottomSheetBehavior getBottomSheetBehaviour() {
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) ((View) getView().getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
return (BottomSheetBehavior) behavior;
}
return null;
}
This just check if the parent of View has 'CoordinatorLayout.LayoutParams' set. If yes, sets appropriate BottomSheetBehavior.BottomSheetCallback (which is needed in the next part), and more importantly returns the CoordinatorLayout.Behavior, which is supposed to be BottomSheetBehavior.
Collapsing:
Here a [`BottomSheetBehavior.BottomSheetCallback.onSlide (View bottomSheet, float slideOffset)``](https://developer.android.com/reference/android/support/design/widget/BottomSheetBehavior.BottomSheetCallback.html#onSlide(android.view.View, float)) is just exactly what is needed. From the [documentation](https://developer.android.com/reference/android/support/design/widget/BottomSheetBehavior.BottomSheetCallback.html#onSlide(android.view.View, float)):
Offset increases as this bottom sheet is moving upward. From 0 to 1 the sheet is between collapsed and expanded states and from -1 to 0 it is between hidden and collapsed states.
This means, that just checking the second parameter is needed for collapse detection:
define BottomSheetBehavior.BottomSheetCallback in the same class:
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
if (slideOffset < 0) {
dismiss();
}
}
};

reduce a Bottom Sheet's height

I know how to change the height of a Bottom Sheet.
There is no problem to increase the height of a bottom sheet. However I cannot decrease its height with the following code .
bottomSheetBehavior.setPeekHeight(peekHeight); // peekHeight < previous height
bottomSheetBehavior.setState(STATE_COLLAPSED);
Anyone encountered the same problem ?
I am trying to do the same and I don't have such problem. I could easily increase and decrease my BottomSheetDialogFragment height. Here is my code of two methods of my fragment:
private BottomSheetBehavior.BottomSheetCallback bottomSheetCallback = new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
}
};
public static BottomSheetFragment newInstance() {
return new BottomSheetFragment();
}
#Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.bottom_sheet_dialog_content_view, null);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams layoutParams = ((CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams());
CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(bottomSheetCallback);
((BottomSheetBehavior) behavior).setPeekHeight(getResources().getDimensionPixelSize(R.dimen.bottom_sheet_height) / 4);
}
initRecyclerView(contentView);
}

Categories

Resources