1、在onCreate()方法中获取
// 没有意义,获取到的结果都为零
int bottomHeight = bottomListLayout.getHeight();
LogUtils.e("bottomHeight: " + bottomHeight);
int bottomMeasuredHeight = bottomListLayout.getMeasuredHeight();
LogUtils.e("bottomMeasuredHeight: " + bottomMeasuredHeight);
int rvHeight = rvList.getHeight();
LogUtils.e("rvHeight: " + rvHeight);
通过以上方式是获取不到的,获取的结果为0。
2、解决方案
获取 ViewTreeObserver 并注册 OnGlobalLayoutListener(当全局布局状态改变或者视图树中视图的可见性发生变化时会进行调用),在这个监听中我们就可以获取 RecyclerView 的高度了
bottomListLayout = findViewById(R.id.bottomListLayout);
sheetBehavior2 = BottomSheetBehavior.from(bottomListLayout);
ViewTreeObserver vto = bottomListLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(
new