I was trying some examples I got for implementing NestedScrollingChild for a custom view.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<com.ayeauto.custom.NestedScrollingChildView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom">
</com.ayeauto.custom.NestedScrollingChildView>
</android.support.design.widget.CoordinatorLayout>
This example works fine and I could see that the ToolBar collapses with the scrolling on NestedScrollingChildView . But if I make the following change then the scrolling on the NestedScrollingChildView has no effect ie. if I scroll over it the ToolBar not collapsing
<com.ayeauto.custom.NestedScrollingChildView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="bottom">
</com.ayeauto.custom.NestedScrollingChildView>
ie, if I give height a size then it is not working. I am using android support library 23.1.1
import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.NestedScrollingChildHelper;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class NestedScrollingChildView extends View implements NestedScrollingChild, GestureDetector.OnGestureListener {
private final NestedScrollingChildHelper mNestedScrollingChildHelper;
private GestureDetectorCompat mDetector;
public NestedScrollingChildView(Context context) {
this(context, null);
}
public NestedScrollingChildView(Context context, AttributeSet attrs) {
this(context, null, 0);
}
public NestedScrollingChildView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mNestedScrollingChildHelper = new NestedScrollingChildHelper(this);
mDetector = new GestureDetectorCompat(context, this);
setNestedScrollingEnabled(true);
}
@Override
public void setNestedScrollingEnabled(boolean enabled) {
mNestedScrollingChildHelper.setNestedScrollingEnabled(true);
}
@Override
public boolean isNestedScrollingEnabled() {
return mNestedScrollingChildHelper.isNestedScrollingEnabled();
}
@Override
public boolean startNestedScroll(int axes) {
return mNestedScrollingChildHelper.startNestedScroll(axes);
}
@Override
public void stopNestedScroll() {
mNestedScrollingChildHelper.stopNestedScroll();
}
@Override
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) {
return mNestedScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
}
@Override
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
return mNestedScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
}
@Override
public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
return mNestedScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
}
@Override
public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
return mNestedScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
mNestedScrollingChildHelper.onDetachedFromWindow();
}
@Override
public boolean hasNestedScrollingParent() {
return mNestedScrollingChildHelper.hasNestedScrollingParent();
}
@Override
public boolean onTouchEvent(MotionEvent event){
final boolean handled = mDetector.onTouchEvent(event);
if (!handled && event.getAction() == MotionEvent.ACTION_UP) {
stopNestedScroll();
}
return true;
}
@Override
public boolean onDown(MotionEvent e) {
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
return true;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
dispatchNestedPreScroll(0, (int) distanceY, null, null);
dispatchNestedScroll(0, 0, 0, 0, null);
return true;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return true;
}
}
I got the code from here It uses android support library 22.2.1, I changed to 23.1.1 because I face this issue for a custom view I created which was working in 22.2.1 but stop working in 23.1.1