Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LinearLayout bottomSheetViewgroup = (LinearLayout) findViewById(R.id.bottomSheet);
- bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetViewgroup);
- bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); //this line
- package uk.ac.qub.quibe.misc;
- import android.content.Context;
- import android.support.design.widget.CoordinatorLayout;
- import android.util.AttributeSet;
- import android.view.View;
- /**
- * Created by mcp on 15/03/16.
- */
- public class ExpandedBottomSheetBehavior<V extends View> extends android.support.design.widget.BottomSheetBehavior<V> {
- public ExpandedBottomSheetBehavior(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- public boolean onLayoutChild(final CoordinatorLayout parent, final V child, final int layoutDirection) {
- SavedState dummySavedState = new SavedState(super.onSaveInstanceState(parent, child), STATE_EXPANDED);
- super.onRestoreInstanceState(parent, child, dummySavedState);
- return super.onLayoutChild(parent, child, layoutDirection);
- /*
- Unfortunately its not good enough to just call setState(STATE_EXPANDED); after super.onLayoutChild
- The reason is that an animation plays after calling setState. This can cause some graphical issues with other layouts
- Instead we need to use setInternalState, however this is a private method.
- The trick is to utilise onRestoreInstance to call setInternalState immediately and indirectly
- */
- }
- }
- app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
- app:layout_behavior="uk.ac.qub.quibe.misc.ExpandedBottomSheetBehavior"
- public class ExpandedBottomSheetBehavior<V extends View> extends
- android.support.design.widget.BottomSheetBehavior<V> {
- public ExpandedBottomSheetBehavior(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- public boolean onLayoutChild(final CoordinatorLayout parent, final V child, final int layoutDirection) {
- return super.onLayoutChild(parent, child, layoutDirection);
- }
- @Override
- public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
- try {
- return super.onInterceptTouchEvent(parent, child, event);
- } catch (NullPointerException ignored) {
- return false;
- }
- }
- }
- final View bottomSheet = findViewById(R.id.bottom_sheet);
- bottomSheet.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
- @Override
- public void onGlobalLayout() {
- bottomSheet.getViewTreeObserver().removeOnGlobalLayoutListener(this);
- bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
- bottomSheetBehavior.setPeekHeight(300);
- bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement