我有一个 RecyclerView
,我在其 View 持有者构造函数中添加了一个 onGlobalLayoutListener,如下所示
public CustomViewHolder(final View itemView, Context context) {
super(itemView, context);
itemView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Get height here
}
});
}
这会为屏幕上可见的所有 itemViews
触发,但是当我滚动 recyclerView 时,它不会为开始出现在屏幕上的 itemViews
触发屏幕。这是为什么?我如何为这些项目捕获此监听器?
最佳答案
像这样注册一个 OnLayoutChangeListener:
imageViewAvatar.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
imageViewAvatar.removeOnLayoutChangeListener(this);
// Get height here
}
});
关于android - RecyclerView itemView OnGlobalLayoutListener 不会为所有 itemView 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33159492/