Skip to content

Instantly share code, notes, and snippets.

@desertjim
Created December 28, 2015 14:22
Show Gist options
  • Save desertjim/ef99215787a7cc4291c2 to your computer and use it in GitHub Desktop.
Save desertjim/ef99215787a7cc4291c2 to your computer and use it in GitHub Desktop.
SmartNestedScrollView
package com.support.android.designlibdemo;
import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
public class SmartNestedScrollView extends NestedScrollView {
public SmartNestedScrollView(Context context) {
super(context);
}
public SmartNestedScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SmartNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = getMeasuredHeight();
int childCount = getChildCount();
int heightNeededToDisplayAllChildren = 0;
boolean shouldScroll = false;
for (int i = 0; i < childCount; i++) {
heightNeededToDisplayAllChildren += getChildAt(i).getHeight();
if(heightNeededToDisplayAllChildren > height){
shouldScroll = true;
break; // no need to go through all the children once the
}
}
setNestedScrollingEnabled(shouldScroll);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment