0

I currently have a CoordinatorLayout with both a ConstraintLayout and a NestedScrollView in it. The NestedScrollView has a peek height of 50dp which is just a title. I want to be able to pull up the NestedScrollView when the device is connected, however when it is not connected I only want to be able to see the peek and not be able to drag up the rest of the view.

if(deviceConnected) {
    mBottomSheetText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            } else {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        }
    });
} else {
        
//Want to prevent it form being draggable

}
ToraCode
  • 421
  • 8
  • 19

1 Answers1

0

The DragCallback interface allows to choose whether the sibling scrolling view should be controlled by scrolls onto the AppBarLayout.

You can do that like below:

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
    @Override
    public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
        return false;
    }
});

By always returning false, your scrolling view will not be controlled by the appbarLayout any longer.

Note: before calling this you should check that ViewCompat.isLaidOut(appBarLayout), otherwise params.getBehavior() will return null.

Check this link.

Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45
  • Sorry this did not work for me. Maybe I am missing something but I am trying to prevent the NestedScrollView from being draggable. Is there a way I could prevent drag events on the CoordinatedView? – Steven Budinic Mar 07 '19 at 01:06