I have a RecyclerView
in whose view holder constructor I'm adding an onGlobalLayoutListener as follows
public CustomViewHolder(final View itemView, Context context) {
super(itemView, context);
itemView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Get height here
}
});
}
This fires for all itemViews
that are visible on the screen, but as I scroll the recyclerView, it doesn't fire for the itemViews
that start to appear on the screen. Why is that? How can I capture this listener for those items?
Register an OnLayoutChangeListener like this:
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
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With