Last active
April 25, 2024 09:56
-
-
Save EmmanuelVinas/c598292f43713c75d18e to your computer and use it in GitHub Desktop.
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.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; | |
} | |
} | |
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
<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> |
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).
I worked with the NestedScrollView for almost 3 hours without a solution, this class saved my life! Thanks a lot
nice job! thanks
thanks a lot!
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
I worked on this problem for hours.
Thank you ! 👍
Thank you!!
I am unable to use it. Please help me for this
very nice, tks!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)