title: Using BottomSheetBehavior with a inner CoordinatorLayout
body: <p>The design support library <code>v. 23.2</code> introduced <code>BottomSheetBehavior</code>, which allows childs of a coordinator to act as bottom sheets (views draggable from the bottom of the screen).</p>
  
  <p>What I’d like to do is to have, <strong>as a bottom sheet view</strong>, the following view (the typical coordinator + collapsing stuff):</p>
  
  <pre><code>&lt;CoordinatorLayout
      app:layout_behavior=“@string/bottom_sheet_behavior”&gt;
  
     &lt;AppBarLayout&gt;
          &lt;CollapsingToolbarLayout&gt;
             &lt;ImageView /&gt;
          &lt;/CollapsingToolbarLayout&gt;
      &lt;/AppBarLayout&gt;
  
      &lt;NestedScrollView&gt;
          &lt;LinearLayout&gt;
              &lt; Content ... /&gt;
          &lt;/LinearLayout&gt;
      &lt;/NestedScrollView&gt;
  
  &lt;/CoordinatorLayout&gt;
  </code></pre>
  
  <p>Unfortunately, bottom sheet views should implement nested scrolling, or they won’t get scroll events. If you try with a main activity and then load this view as a bottom sheet, you’ll see that scroll events only act on the “sheet” of paper, with some strange behavior, as you can see if you keep reading.</p>
  
  <p>I am pretty sure that this can be handled by subclassing <code>CoordinatorLayout</code>, or even better by subclassing <code>BottomSheetBehavior</code>. Do you have any hint?</p>
  
  <h2>Some thoughts</h2>
  
  <ul>
  <li><p><code>requestDisallowInterceptTouchEvent()</code> should be used, to steal events from the parent in some conditions:</p>
  
  <ul>
  <li>when the <code>AppBarLayout</code> offset is > 0</li>
  <li>when the <code>AppBarLayout</code> offset is == 0, but we are scrolling up (think about it for a second and you’ll see)</li>
  </ul></li>
  <li><p>the first condition can be obtained by setting an <code>OnOffsetChanged</code> to the inner app bar;</p></li>
  <li><p>the second requires some event handling, for example:</p>
  
  <pre><code>switch (MotionEventCompat.getActionMasked(event)) {
      case MotionEvent.ACTION_DOWN:
          startY = event.getY();
          lastY = startY;
          userIsScrollingUp = false;
          break;
      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
          userIsScrollingUp = false;
          break;
      case MotionEvent.ACTION_MOVE:
          lastY = event.getY();
          float yDeltaTotal = startY - lastY;
          if (yDeltaTotal &gt; touchSlop) { // Moving the finger up.
              userIsScrollingUp = true;
          }
          break;
  }
  </code></pre></li>
  </ul>
  
  <h2>Issues</h2>
  
  <p>Needless to say, I can’t make this work right now. I am not able to catch the events when the conditions are met, and not catch them in other cases. In the image below you can see what happens with a standard CoordinatorLayout:</p>
  
  <ul>
  <li><p>The sheet is dismissed if you scroll down on the appbar, but not if you scroll down on the nested content. It seems that nested scroll events are not propagated to the Coordinator behavior;</p></li>
  <li><p>There is also a problem with the inner appbar: the nested scroll content does not follow the appbar when it is being collapsed..</p></li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/nZBle.gif"><img src="https://i.stack.imgur.com/nZBle.gif" alt="enter image description here"></a></p>
  
  <p>I have setup a <a href="https://github.com/miav/BottomSheetCoordinatorLayout">sample project on github</a> that shows these issues.</p>
  
  <h2>Just to be clear, desired behavior is:</h2>
  
  <ul>
  <li><p>Correct behavior of appbars/scroll views inside the sheet;</p></li>
  <li><p>When sheet is expanded, it can collapse on scroll down, but <strong>only if the inner appbar is fully expanded too</strong>. Right now it does collapse with no regards to the appbar state, and only if you drag the appbar;</p></li>
  <li><p>When sheet is collapsed, scroll up gestures will expand it (with no effect on the inner appbar).</p></li>
  </ul>
  
  <p>An example from the contacts app (which probably does not use BottomSheetBehavior, but this is what I want):</p>
  
  <p><a href="https://i.stack.imgur.com/WWLcN.gif"><img src="https://i.stack.imgur.com/WWLcN.gif" alt="enter image description here"></a></p>