简体   繁体   中英

dynamically create array with all children inside of a linearlayout

This is the code I have:

package mika.actual;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class accordion extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accordion);

        final View panels[] = {
            findViewById(R.id.panelProfile),
            findViewById(R.id.panelSettings),
            findViewById(R.id.panelPrivacy)
        };

        final Button buttons[] = {
            (Button) findViewById(R.id.btnProfile),
            (Button) findViewById(R.id.btnSettings),
            (Button) findViewById(R.id.btnPrivacy)
        };

        for (int i = 0; i < panels.length; i++){
            panels[i].setVisibility(View.GONE);
        }

        for (int i = 0; i < buttons.length; i++){
            final int x = i;

            buttons[i].setBackgroundColor(getResources().getColor(R.color.button_not_pressed));
            buttons[i].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int y = 0; y < panels.length; y++){
                        panels[y].setVisibility(View.GONE);
                    }
                    panels[x].setVisibility(View.VISIBLE);


                    for (int y = 0; y < buttons.length; y++){
                        buttons[y].setBackgroundColor(getResources().getColor(R.color.button_not_pressed));
                    }
                    buttons[x].setBackgroundColor(getResources().getColor(R.color.button_pressed));
                }
            });
        }
    }
}

and XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFFFF"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnProfile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Profile"
        android:textColor="#FFFFFFFF" />

        <LinearLayout
        android:id="@+id/panelProfile"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FFFFFFFF">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFFFF">

                <LinearLayout
                    android:id="@+id/panelProfile1"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFFFF">

                    <TextView
                        android:id="@+id/strName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Name" />

                    <EditText
                        android:id="@+id/txtName"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>


                <LinearLayout
                    android:id="@+id/panelProfile2"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFFFF">

                    <TextView
                        android:id="@+id/strSurname"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Surname" />

                    <EditText
                        android:id="@+id/txtSurname"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>

            </LinearLayout>

        </ScrollView>

    </LinearLayout>



    <Button
        android:id="@+id/btnSettings"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Settings"
        android:textColor="#FFFFFFFF" />

    <LinearLayout
        android:id="@+id/panelSettings"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FFFFFFFF">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/panelSettings1"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFFFF">

                    <TextView
                        android:id="@+id/strMail"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="e-mail" />

                    <EditText
                        android:id="@+id/txtMail"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/panelSettings2"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFFFF">

                    <TextView
                        android:id="@+id/strPhone"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Phone" />

                    <EditText
                        android:id="@+id/txtPhone"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>

            </LinearLayout>

        </ScrollView>

    </LinearLayout>



    <Button
        android:id="@+id/btnPrivacy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Privacy"
        android:textColor="#FFFFFFFF" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/panelPrivacy"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#FFFFFFFF">

            <CheckBox
                android:id="@+id/checkFacebook"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Facebook"
                android:textColor="#ff355689">
            </CheckBox>
            <CheckBox
                android:id="@+id/checkLinkedIn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="LinkedIn"
                android:textColor="#ff355689">
            </CheckBox>
            <CheckBox
                android:id="@+id/checkTwitter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Twitter"
                android:textColor="#ff355689">
            </CheckBox>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

I want to create the two arrays buttons[] and panels[] dynamically, taking all the child views inside the LinarLayout (with the id root_layout). Any suggestions appreciated. Thanks in advance :)

Maybe iterate over the childrens of your Viewgroup and adding them in a Arraylist?:

LinearLayout rootView = (LinearLayout) findViewById(R.id.mylinearlayoutid);
ArrayList<Button> buttons = new ArrayList<>(rootView.getChildCount());
for(int i = 0; i < rootView.getChildCount(); i++){
    buttons.add ((Button) rootView.getChildAt(i));        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM