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> |
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
Thank you!!