Android Получить все дочерние элементы, которые есть у Layout (включая дочерних) - PullRequest
0 голосов
/ 01 июля 2018

По сути, я пытаюсь создать функцию с рекурсивным вызовом , чтобы я мог получить все RadioButtons, Buttons, FAB, TextView и даже другие SubViews ( LinearLayouts, RelativeLayouts, FrameLayout, ViewGroup ). Прямо сейчас я могу получить все макеты, но когда я хочу получить доступ к подконтенту этих subViews , эти значения никогда не возвращаются ...

var contentList = ArrayList<String>()

fun ManageView(viewGroup: ViewGroup, action: String, context: Activity){
    try {
        loop@ for (i in 0 until viewGroup.childCount) {
            var child = viewGroup.getChildAt(i)
            contentList.add(context.applicationContext.resources.getResourceEntryName(child.id))
            when (child) {
                is ViewGroup->{
                    ManageView(child,"vacant",context)
                }
                is RadioGroup -> {
                    contentList.add(context.applicationContext.resources.getResourceEntryName(child.id))
                }
            }
        }
        println("TOTAL OF ELEMENTS: " + contentList.size)
        contentList.forEach { list ->
            println("Data:  " + list)
        }
    }catch (exception: Exception){
        println("Houston we have a layout problem")
    }
}

и значения, которые я получил:

07-01 07:35:40.543 30090-30090/com.epictech.epiccab_driver I/System.out: TOTAL OF ELEMENTS: 18
Data:  framePrice
Data:  lblRate
Data:  viewInclude
Data:  layoutPaidHired
Data:  viewSeparator
Data:  idPaidSign
Data:  txtPaidResult
Data:  viewSeparatorRight
Data:  containerHiredDetails
Data:  layoutHiredPays
Data:  viewSeparatorPaidRight
Data:  frameTripRateInfo
07-01 07:35:40.544 30090-30090/com.epictech.epiccab_driver I/System.out: Data:  fragment_fragment_trip_info
    Data:  frameContainer
    Data:  layoutDriverData
    Data:  menu
    Data:  layoutFAB
    Data:  layoutSpeedometer

Однако в моем xml я получил много контента, в котором есть сочетание FrameLayout, Relative и Linear. Это пример:

MainActivity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainView"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/transparent"
    tools:context="com.epictech.epiccab_driver.UI.MainActivity"
    android:keepScreenOn="true">

    <RelativeLayout
        android:id="@+id/framePrice"
        android:layout_width="510dp"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:layout_toEndOf="@+id/layoutDriverData">

        <TextView
            android:id="@+id/lblRate"
            android:layout_width="match_parent"
            android:layout_height="27dp"
            android:gravity="start|center_vertical"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:background="?attr/colorPrimary"
            android:textColor="@color/md_black_1000"
            android:textSize="12sp"
            android:text="@string/activity_main_title_rate" />

        <include
            android:id="@+id/viewInclude"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_below="@+id/lblRate"
            layout="@layout/fragment_paid_hired" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/frameTripRateInfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimaryDark"
        android:visibility="gone"
        android:layout_toEndOf="@+id/framePrice"
        android:baselineAligned="false">

        <include
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            layout="@layout/fragment_fragment_hired_trip_info" />
    </RelativeLayout>

    <FrameLayout
        android:id="@+id/frameContainer"
        android:layout_width="match_parent"
        android:layout_above="@+id/menu"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/frameTripRateInfo"
        android:layout_toEndOf="@+id/layoutDriverData">
    </FrameLayout>

    <RelativeLayout
        android:id="@+id/layoutDriverData"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="?attr/backgroundColor"
        android:layout_above="@+id/menu">
        <include
            layout="@layout/card_driver_status"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/menu"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_alignParentBottom="true">
        <include
            layout="@layout/menu_layout"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layoutFAB"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/layoutSpeedometer"
        android:layout_above="@+id/menu"
        android:layout_alignParentEnd="true">
        <include
            layout="@layout/fab_map"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layoutSpeedometer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true">

    </RelativeLayout>
</RelativeLayout>

и это MenuLayout , которые MainActivity включают

MenuLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp">

    <FrameLayout
        android:id="@+id/frameLayouTripInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone">
        <include layout="@layout/fragment_trip_info" />
    </FrameLayout>

    <View
        android:id="@+id/viewSeparatorTop"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/frameLayouTripInfo"
        android:background="@color/md_white_1000" />

    <RadioGroup
        android:id="@+id/menuGroup"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_below="@+id/viewSeparatorTop"
        android:orientation="horizontal"
        android:background="?attr/backgroundColor"
        android:weightSum="1"
        android:divider="@drawable/divider"
        android:dividerPadding="7dip"
        android:showDividers="middle">

        <RadioButton
            android:id="@+id/radioBtnStartTrip"
            style="@style/RadioButtonState"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:layout_weight="0.14285714"
            android:text="@string/start_trip" />

        <RadioButton
            android:id="@+id/radioBtnAddToll"
            style="@style/RadioButtonState"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:layout_weight="0.14285714"
            android:text="@string/add_toll" />

        <RadioButton
            android:id="@+id/radioBtnAddExtras"
            style="@style/RadioButtonState"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:layout_weight="0.14285714"
            android:text="@string/add_extras" />

        <RadioButton
            android:id="@+id/radioBtnOffDuty"
            style="@style/RadioButtonState"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:layout_weight="0.14285714"
            android:text="@string/off_duty" />


        <LinearLayout
            android:id="@+id/emailBtn"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:layout_weight="0.14285714"
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="9dp"
            android:background="?attr/backgroundColor">

            <RelativeLayout
                android:id="@+id/layoutEmail"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:background="@drawable/email"
                android:backgroundTint="@color/md_black_1000"
                android:layout_gravity="center">

                <com.epictech.epiccab_driver.Helper.Notification.CounterTextView
                    android:id="@+id/notification_count"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="19dp"
                    android:gravity="center"
                    android:paddingBottom="2dp"
                    android:paddingLeft="4dp"
                    android:paddingRight="4dp"
                    android:paddingTop="22dp"
                    android:textColor="@android:color/white"
                    android:textSize="20sp"
                    app:ctv_rounded_background_color="@color/md_red_600"
                    tools:text="99+" />
            </RelativeLayout>
        </LinearLayout>

        <ImageView
            android:id="@+id/settings"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            app:srcCompat="@drawable/settings"
            android:padding="7dp"
            android:backgroundTint="?attr/primaryTextColor"
            android:layout_gravity="center"
            android:scaleType="fitCenter"
            android:layout_weight="0.14285714"
            android:contentDescription="@string/configurations" />

        <RadioButton
            android:id="@+id/radioBtnEndShift"
            style="@style/RadioButtonState"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:layout_weight="0.14285714"
            android:text="@string/end_shift" />

    </RadioGroup>
</RelativeLayout>

Пожалуйста, скажите мне, что я делаю не так ...

Спасибо за поддержку !!

1 Ответ

0 голосов
/ 01 июля 2018

Чтобы собрать все RadioButton представления (как указано в вашем примере), просто измените ваш код следующим образом. Чтобы собрать больше типов, поместите их в один и тот же блок when:

var contentList = ArrayList<String>()
fun ManageView(view: View, action: String, context: Activity) {

    try {
        // if you want all the views then add elements to the list without any when
        when (view) {
            is RadioGroup -> {
                loop@ for (j in 0 until view.childCount){
                    val child = view.getChildAt(j)
                    contentList.add(context.applicationContext.resources.getResourceEntryName(child.id))
                }
            } 
            //add more cases here to collect more types 
            // all the view types that you'd like to collect must be put here
        }
        println("TOTAL OF ELEMENTS: " + contentList.size)
        contentList.forEach { list ->
            println("Data:  " + list)
        }
        if (view is ViewGroup) {
            loop@ for (i in 0 until view.childCount) {
                val child = view.getChildAt(i)
                when (child) {
                    is ViewGroup -> {
                        ManageView(child, "vacant", context)
                    }
                }
            }
        }
    }catch (exception: Exception){
        println("Houston we have a layout problem")
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...