Android開發面試題之遍歷ViewGroup拿到所有的ViewGroup和View的id

面試題如題:

咱們老套路先上圖:下面是我通過遍歷拿到的所有的id,怎麼做的呢?

咱們先說下思路:

首先拿到最外層的ViewGroup然後通過它拿到它所有的child然後循環每個child判斷是ViewGroup還是View,如果是ViewGroup就繼續遍歷(遞歸),不是VieGroup的話那就是View了,那就直接打印View的id即可。

看代碼吧。

 //遍歷樹形結構view
        ViewGroup llRoot = findViewById(R.id.ll_root);
        forData(llRoot);
   

/**
     * 遍歷ViewGroup的方法
     *
     * @param llRoot 根VieGroup
     */
    private void forData(ViewGroup llRoot) {
        int childCount = llRoot.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (llRoot.getChildAt(i) instanceof ViewGroup) {
                Log.e("打印ViewGroup的id", llRoot.getChildAt(i).getId() + "=");
                forData((ViewGroup) llRoot.getChildAt(i));
            } else {
                Log.e("打印View的id", llRoot.getChildAt(i).getId() + "=");
            }
        }
    }

咱們再看下xml佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:inputType="text"
        android:text="408" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showResult"
        android:text="通過MVP模式顯示結果" />

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

        <LinearLayout
            android:id="@+id/tw_three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

            <TextView
                android:id="@+id/tv_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <TextView
            android:id="@+id/tv_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/sv_four"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/tv_shoe_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black" />

    </ScrollView>

</LinearLayout>

懂了吧。

發佈了193 篇原創文章 · 獲贊 107 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章