package org.awesomeapp.messenger.ui.widgets;
import android.content.Context;
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;
/**
* Created by n8fr8 on 10/25/15.
* From: http://stackoverflow.com/questions/30801379/coordinatorlayout-with-collapsingtoolbarlayout-breaks-with-keyboard-in-dialog-fr
*/
public class PatchedScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior {
public PatchedScrollingViewBehavior() {
super();
}
public PatchedScrollingViewBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
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() + scrollRange;
int parentHeight = View.MeasureSpec.getSize(parentHeightMeasureSpec);
int height = parentHeight - appBar.getMeasuredHeight() + scrollRange;
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);
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;
}
}