如何在Android中逐个添加连续的布局行

时间:2014-09-02 14:49:15

标签: java android android-layout android-linearlayout

这是我第一次在这里问一些事情:)

让我们来看看主题:我正在尝试做一些典型的“点击这里添加另一个人”。 “人”是另一个字段,所以我已经有4行用于输入名称,但我想逐个添加到8行(另外四个,每次点击“按钮”一个)。

调试:当我按下布局时,它将显示第5个贡献者,但如果我再次点击,它将不会出现任何其他贡献者。知道如何解决这个问题吗?

这是我的“.xml”:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dip" >

    <!-- Layout del form -->
    <LinearLayout
        android:id="@+id/InputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.85"
        android:orientation="vertical" >

        [...] //Some Layouts

        <!-- Contributor 1 Layout -->

        <LinearLayout
            android:id="@+id/ContributorLayout_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/NameLayout"
            android:layout_marginBottom="15dip"
            android:orientation="horizontal" >

            <!-- Name Label -->

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.45"
                android:text="@string/Contributor_Name1_Str"
                android:textSize="17dip" />

            <!-- Input Name -->

            <EditText
                android:id="@+id/inputPart_1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dip"
                android:layout_weight="0.55"
                android:singleLine="true" />
        </LinearLayout>


        <!-- Contributor 2 Layout -->

        [...] //Just as the Contributor 1


        <!-- Contributor 3 Layout -->

        [...] //Just as the Contributor 1


        <!-- Contributor 4 Layout -->

        [...] //Just as the Contributor 1


        <!-- Contributor 5 Layout -->

        <LinearLayout
            android:id="@+id/ContributorLayout_5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ContributorLayout_4"
            android:layout_marginBottom="15dip"
            android:orientation="horizontal"
            android:visibility="gone" >

            <!-- Name Label -->

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.45"
                android:text="@string/Contributor_Name5_Str"
                android:textSize="17dip" />

            <!-- Input Name -->

            <EditText
                android:id="@+id/inputContributor_5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dip"
                android:layout_weight="0.55"
                android:singleLine="true" />
        </LinearLayout>

        <!-- Contributor 6 Layout -->

        [...] //Just as the Contributor 5 // AKA Visibility = GONE

        <!-- Contributor 7 Layout -->

        [...] //Just as the Contributor 5 // AKA Visibility = GONE

        <!-- Contributor 8 Layout -->

        [...] //Just as the Contributor 5 // AKA Visibility = GONE

    </LinearLayout>


    <!-- Layout Inferior del form. -->
    <LinearLayout
        android:id="@+id/SubLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/InputLayout"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/Add_Colab_Field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dip"
            android:paddingLeft="25dip"
            android:paddingRight="25dip"
            android:orientation="horizontal"
            android:clickable="true" >

            <ImageView
                android:id="@+id/add_event"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:layout_gravity="left"
                android:src="@drawable/ic_add_colab" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.75"
                android:layout_marginLeft="20dip"
                android:text="@string/Add_Colab_Field"
                android:textSize="17dip" />

        </LinearLayout>
        <!-- Button Create Event -->

        <Button
            android:id="@+id/btnAddEvent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Add_Colab_Field"
            android:text="@string/Add_Event_Bttn" />
    </LinearLayout>

</LinearLayout>

这是我的“班级”:

// Click en el botón add_colab_field
    LinearLayout add_colab_field = (LinearLayout) findViewById(R.id.Add_Colab_Field);
    add_colab_field.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            final LinearLayout colab5 = (LinearLayout) findViewById(R.id.ContributorLayout_5);
            final LinearLayout colab6 = (LinearLayout) findViewById(R.id.ContributorLayout_6);
            final LinearLayout colab7 = (LinearLayout) findViewById(R.id.ContributorLayout_7);
            final LinearLayout colab8 = (LinearLayout) findViewById(R.id.ContributorLayout_8);
            // Si presionamos el botón añadir...
            // Añadimos una nueva línea de participante

            if (colab5.getVisibility() == View.GONE) {
                colab5.setVisibility(View.VISIBLE);
            } else if (colab5.getVisibility() == View.VISIBLE
                    && colab6.getVisibility() == View.GONE) {
                colab6.setVisibility(View.VISIBLE);
            } else if (colab6.getVisibility() == View.VISIBLE
                    && colab7.getVisibility() == View.GONE) {
                colab7.setVisibility(View.VISIBLE);
            } else if (colab7.getVisibility() == View.VISIBLE
                    && colab8.getVisibility() == View.GONE) {
                colab8.setVisibility(View.VISIBLE);
            }

        }
    });

最好的问候,吉列尔莫。

2 个答案:

答案 0 :(得分:0)

您正在检查coblab5可见性两次。根据您的逻辑,您应该else if (colab5.getVisibility() == View.VISIBLE && colab6.getVisibility() == View.GONE) colab6.setVisibility(View.VISIBLE);

并考虑@Alexander Zhak的评论。

答案 1 :(得分:0)

第一次else if检查一个永远不会成立的条件:colab5无法同时显示。您可能打算为第二个条件写colab6