先上效果图:

如果顶部是单个ImageView我们可以用下面的方法来获取:
ViewTreeObserver vto = image_view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
title.getViewTreeObserver().removeOnGlobalLayoutListener(this);
height = image_view.getHeight() - (head_layout.getHeight() + 20);
}
});
如果是多布局复杂的Recyclerview,banner一般位于最顶部,那么recyclerview第0个位置上的item高度又如何获取呢
@Override
public void setData(List<News.Data.New> datas) {
mData.addAll(datas);
mAdapter.notifyDataSetChanged();
getBannerHeight();
}
public void getBannerHeight() {
new Handler().postDelayed(() -> {
try {
ViewGroup item = (ViewGroup) rcy.getLayoutManager().getChildAt(0);
View at = item.getChildAt(0);
int height = at.getHeight();
Log.e(TAG, "height========: " + height);
int barHeight = mActionBar.getHeight();
if (height - barHeight > 0)
duration = height - barHeight;
Log.e(TAG, "d========: " + duration);
} catch (Exception e) {
Log.e(TAG, "getBannerHeight: 异常了。。。。。。。。。。。。。");
getBannerHeight();
}
}, 1500);
}
简单说明一下:notifyDataSetChanged之后,先获取到第0个itemview,再通过 view.getHeight()方法得到banner的高度。再用BannerHeight - ActionbarHeight得到我们的滑动范围duration。
这里为什么要延迟1.5秒获取?是因为在notifyDataSetChanged之后立即获取高度的话,item.getChildAt(0)可能会返回空,至于为什么要用异常递归,就是为了防止item.getChildAt空时获取不到,直至获取到再接束。
ps.上述方法虽然可以实现效果,但感觉不优雅,如果哪位大神有好的方法,请留言与我交流
最后一张图解释下 duration = height - barHeight;

网友评论
//获取顶部title的高度
mActionbar.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
height = mActionbar.getMeasuredHeight();
BLog.i("----------height=" + height);
return true;
}
});
---------------------
内容来自 王的博客 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/Wang_WY/article/details/77665117?utm_source=copy
addScorllListenrer,上下滑动 y 轴变化 衡等于0,所以你只需要订好阀值即可
@Override
public void onScrolled (RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
scrollHeight += dy;
if (scrollHeight >= scrollMax && !commentBarAlreadyShow) {
operatorCommentNumBar(true);
commentBarAlreadyShow = true;
} else if (scrollHeight < scrollMax && commentBarAlreadyShow) {
operatorCommentNumBar(false);
commentBarAlreadyShow = false;
}
}
});