Created
December 28, 2015 14:22
-
-
Save desertjim/ef99215787a7cc4291c2 to your computer and use it in GitHub Desktop.
SmartNestedScrollView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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