0

Je suis vraiment préoccupé par la méthode .findViewById() et son utilisation dans une création de vue composée personnalisée. Je ne suis pas sûr de l'endroit exact où il est garanti qu'il ne reviendra jamais null.Vue composée personnalisée et .findViewById()

Disons que j'ai ce point de vue composé personnalisé et il est ajouté à certains .xml comme ceci:

<com.app.path.TwoButtonsView 
      android:id="@+id/ok_cancel_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

two_buttons_view.xml:

<?xml version="1.0" encoding="utf-8"?> 
<merge> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/first_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:gravity="center" 
      android:textAllCaps="true"/> 

     <TextView 
      android:id="@+id/second_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:gravity="center" 
      android:textAllCaps="true"/> 
    </LinearLayout> 
</merge> 

TwoButtonsView.java

public class TwoButtonsView extends LinearLayout { 

    // views 
    private TextView mFirstButtonView; 
    private TextView mSecondButtonView; 

    public TwoButtonsView(Context context) { 
     super(context); 
     init(context, null); 
    } 

    public TwoButtonsView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context, attrs); 
    } 

    public TwoButtonsView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context, attrs); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public TwoButtonsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
     init(context, attrs); 
    } 

    private void init(Context context, AttributeSet attrs) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.two_buttons_view, this); 

     // retrieve views 
     mFirstButtonView = (TextView) findViewById(R.id.first_button); // is there a chance it will return null? 
     mSecondButtonView = (TextView) findViewById(R.id.second_button); // is there a chance it will return null? 
    } 
} 

Ma question est: est-il une chance que .findViewById(RESOURCE_ID) retournera null juste après avoir appelé inflater.inflate(R.layout.two_buttons_view, this);, ou devrais-je appeler ma méthode init() sur onFinishInflate() rappel?

Répondre

2

Ma question est: est-il possible que .findViewById (RESOURCE_ID) retournera droit nul après avoir appelé inflater.inflate (R.layout.two_buttons_view, ce)

Non, il n'y non, tant que les vues que vous recherchez figurent dans la mise en page et que vous les avez explicitement ajoutées à la hiérarchie de la vue. À la fin, findViewById boucles sur View[], rempli par addView.

Une petite note sur

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
inflater.inflate(R.layout.two_buttons_view, this); 

ViewGroup a la méthode statique inflate, de sorte que vous n'avez pas besoin de récupérer le inflater appelant getSystemService

+0

Merci beaucoup pour votre réponse, @Blackbelt! Pourriez-vous m'expliquer quel est le but de 'onFinishInflate()' alors, s'il vous plaît? –

+1

c'est un callback qui obtient un appel quand tous les enfants déclarés dans la mise en page ont été ajoutés. Dans votre cas, vous ajoutez manuellement – Blackbelt

+0

Merci encore pour la réponse: D. Une dernière question: vous avez dit que je les ai ajoutés manuellement, mais quelle est l'autre façon de les ajouter "pas manuellement"? Je ne pouvais pas penser à = = –