Skip to content

Instantly share code, notes, and snippets.

@EmmanuelVinas
Last active April 25, 2024 09:56
Show Gist options
  • Star 46 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save EmmanuelVinas/c598292f43713c75d18e to your computer and use it in GitHub Desktop.
Save EmmanuelVinas/c598292f43713c75d18e to your computer and use it in GitHub Desktop.
package com.evs.demo.layout;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import java.util.List;
public class FixedScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior{
public FixedScrollingViewBehavior() {
}
public FixedScrollingViewBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean onMeasureChild(CoordinatorLayout parent, View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
if(child.getLayoutParams().height == -1) {
List dependencies = parent.getDependencies(child);
if(dependencies.isEmpty()) {
return false;
}
AppBarLayout appBar = findFirstAppBarLayout(dependencies);
if(appBar != null && ViewCompat.isLaidOut(appBar)) {
if(ViewCompat.getFitsSystemWindows(appBar)) {
ViewCompat.setFitsSystemWindows(child, true);
}
int scrollRange = appBar.getTotalScrollRange();
int height = parent.getHeight() - appBar.getMeasuredHeight() + Math.min(scrollRange, parent.getHeight()- heightUsed);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
parent.onMeasureChild(child, parentWidthMeasureSpec, widthUsed, heightMeasureSpec, heightUsed);
return true;
}
}
return false;
}
private static AppBarLayout findFirstAppBarLayout(List<View> views) {
int i = 0;
for(int z = views.size(); i < z; ++i) {
View view = (View)views.get(i);
if(view instanceof AppBarLayout) {
return (AppBarLayout)view;
}
}
return null;
}
}
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.evs.demo.layout.FixedScrollingViewBehavior">
.....
</android.support.v4.widget.NestedScrollView>
@AnilKumarJha
Copy link

I am unable to use it. Please help me for this

@Gperez88
Copy link

very nice, tks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment