Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView itemView OnGlobalLayoutListener doesn't fire for all itemViews

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?

like image 964
Aks Avatar asked Oct 15 '15 22:10

Aks


1 Answers

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
}
});
like image 191
Milan Avatar answered Oct 23 '22 22:10

Milan