Skip to content

Instantly share code, notes, and snippets.

@EmmanuelVinas
Last active April 25, 2024 09:56
Show Gist options
  • 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>
@EmmanuelVinas
Copy link
Author

@guillermomuntaner
Copy link

Hi, nice fix. However, when the scroll content is smaller than the scroll itself, the scroll still responds and collapses the toolbar. Any idea how to just disable the scroll and the collapse when the scroll is not needed at all (for small content scenarios)

@EmmanuelVinas
Copy link
Author

Hi, I haven't tried this yet but I guess you should be able to do it by overriding the behavior on the AppBarLayout (android.support.design.widget.AppBarLayout.ScrollingViewBehavior).

@j1c1m1b1
Copy link

I worked with the NestedScrollView for almost 3 hours without a solution, this class saved my life! Thanks a lot

@erikjhordan-rey
Copy link

nice job! thanks

@pengx1n
Copy link

pengx1n commented Aug 14, 2015

thanks a lot!

@engr-erum
Copy link

how this will work in this layout ? i want a xml like : RelativeLayout + Linearlayout + Design Tab Layout + viewpager (Now ViewPager contains Fragment for each Tab ? ) but now i want once i try to scroll any tabLayout (Fragment) it should scroll from top or with top means tab will also move

@MehdiChouag
Copy link

I worked on this problem for hours.

Thank you ! 👍

@androidroadies
Copy link

Thank you!!

@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