1

J'essaie de compter toutes les vues dans n'importe quelle disposition affichée à l'utilisateur. Pour y parvenir, le code affiché ci-dessous a été utilisé, mais comme vous le voyez dans la mise en page, il contientComment obtenir le nombre total de vues dans un lyout, y compris les sous-vues

Button 
    LinearLayout 
     Button 
     Button 
    TextView 
    LinearLayout 

lorsque le code est exécuté, il est dit que la mise en page a seulement 4 widgets « Button, LinearLayout, TextView, LinearLayout ", alors que je m'attendais à ce que le code renvoie 6 widgets. pourriez-vous s'il vous plaît me dire comment obtenir le total des widgets contient dans une mise en page, y compris les sous-widgets/vues?

Code:

public class ActMain extends AppCompatActivity { 

private static final String TAG = ActMain.class.getSimpleName(); 
private LinearLayout mMainContainer = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.act_main); 

    this.mMainContainer = (LinearLayout) findViewById(R.id.actMain_mainContainer); 
    int totalWidgets = this.mMainContainer.getChildCount(); 
    Log.d(TAG, "totalWidgets:" + totalWidgets); 


    for (int i = 0; i < this.mMainContainer.getChildCount(); i++) { 
     View view = mMainContainer.getChildAt(i); 
    } 
} 

}

mise en page:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/actMain_mainContainer" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.example.alten.test_traversethroughaview_1.ActMain"> 

    <Button 
     android:id="@+id/actMain_btn_change" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/decision_change" /> 

     <LinearLayout 
      android:id="@+id/actMain_linlay_1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
      <Button 
       android:id="@+id/actMain_btn_yes" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/decision_yes" /> 
      <Button 
       android:id="@+id/actMain_btn_no" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/decision_no" /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/actMain_tv_display" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <LinearLayout 
      android:id="@+id/actMain_linlay_2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
     </LinearLayout> 

Répondre

0

utilisent cette méthode:

public int getChildrenViews(ViewGroup parent){ 
    int count = parent.getChildCount(); 
    for (int i=0;i<parent.getChildCount();i++){ 
     if (parent.getChildAt(i) instanceof ViewGroup){ 
      count+=getChildrenViews((ViewGroup) parent.getChildAt(i)); 
     } 
    } 
    return count; 
} 

où vous voulez ajouter cette partie: merci

if (getWindow().getDecorView().getRootView() instanceof ViewGroup){ 
     getChildrenViews((ViewGroup) getWindow().getDecorView().getRootView()); 
    } 
+0

pour tha réponse. mais existe-t-il un autre moyen de voir le groupe principal/conteneur principal qui est actuellement affiché sur l'écran? – LetsamrIt

+0

@LetsamrIt utilisez cette ligne pour accéder à la disposition principale de l'écran en cours: getWindow(). GetDecorView(). GetRootView(); comme ci-dessous: 'if (getWindow(). GetDecorView(). GetRootView() instanceof ViewGroup) { getChildrenViews ((ViewGroup) getWindow(). GetDecorView(). GetRootView()); } ' J'ai changé la réponse principale. –

0

Votre code ne comptant que son enfant parent, pas tout l'enfant dans le lay en dehors. Cela signifie que vous comptez l'enfant de Grand-père, pas le petit-fils de Grand-père. Vous devez traverser toutes les vues pour compter tous les enfants. Pour ce faire, vous pouvez utiliser une fonction récursive pour compter tous les enfants.

getChildCount(View) 
    count = 1; 
    check the view is instance of ViewGroup 
    if it is ViewGroup 
    for each child 
     count+= getChildCount(child) 

    return count 

Appelez ce avec la mise en page de racine

totalChild = getChildCount(root) 

Espérons que vous pouvez compter tous les enfants de cette façon.

0

Cette fonction sera probablement aider:

private int countChild(View view) { 
    if (!(view instanceof ViewGroup)) 
     return 1; 

    int counter = 0; 

    ViewGroup viewGroup = (ViewGroup) view; 

    for (int i=0; i<viewGroup.getChildCount(); i++) { 
     counter += countChild(viewGroup.getChildAt(i)); 
    } 

    return counter; 
} 

Il ne compte que les feuilles (ne tient pas compte des ViewGroups). Mais si vous voulez aussi les compter si c'est simple: remplacez simplement counter = 0 par counter = 1.