2015-10-15 14 views
9

Ho un RecyclerView in cui visione titolare costruttore sto aggiungendo un onGlobalLayoutListener come segueRecyclerView itemView OnGlobalLayoutListener non si attiva per tutti i itemViews

public CustomViewHolder(final View itemView, Context context) { 
    super(itemView, context); 
    itemView.getViewTreeObserver().addOnGlobalLayoutListener(
     new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
      // Get height here 
      } 
    }); 
} 

Questo fuochi per tutto itemViews che sono visibili sullo schermo, ma mentre scorro il recyclerView, non si accende per lo itemViews che inizia ad apparire sullo schermo. Perché? Come posso catturare questo ascoltatore per quegli oggetti?

+0

esclusione 'onLayout' del' itemView' – pskink

risposta

8

Pubblicare un OnLayoutChangeListener come questo:

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 
} 
}); 
+3

Dammi motivi per cui dovrei usare questo invece di 'ViewTreeObserver', per favore. – deadfish

Problemi correlati