如何在动态视图中添加/获取数据?

时间:2018-08-11 15:18:58

标签: android android-layout android-view

我有ID为“ layout”的父级线性布局(位于滚动视图中), 在此父布局(线性布局)内部,我生成自定义约束布局,该布局具有许多视图(Textview,图像,按钮,editText..etc)。

这是父视图的布局:

 <?xml version="1.0" encoding="utf-8"?>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:id="@+id/linear">

        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:fillViewport="true"
            android:id="@+id/scroll">


            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/layout"
                android:orientation="vertical"
                <!--inside it will be many constraint view childs-->
                />
        </ScrollView>

        <Button
            android:layout_width="70dp"
            android:layout_height="30dp"
            android:background="@drawable/red_rounded_btn"
            android:layout_margin="8dp"
            android:padding="4dp"
            android:id="@+id/finish_btn"
            android:textColor="@color/colorPrimary"
            android:layout_gravity="right"
            android:gravity="center_horizontal"
            android:text="Finish" />
    </LinearLayout>

这是我的自定义约束布局(子级):

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/child">


    <ImageView
        android:id="@+id/imageView_field"
        android:layout_width="138dp"
        android:layout_height="138dp"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toStartOf="@+id/uploud_field_pic_btn"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <Button
        android:id="@+id/uploud_field_pic_btn"
        android:layout_width="185dp"
        android:layout_height="35dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="164dp"
        android:background="#1aB00020"
        android:drawableRight="@drawable/uploud_icon"
        android:text="@string/insert_picture"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView_size_custom_reg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/size"
        android:textColor="@color/colorPrimaryDark"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="@+id/custom_name_reg"
        app:layout_constraintTop_toBottomOf="@+id/custom_name_reg" />

    <EditText
        android:id="@+id/textView_size_in_reg_custom"
        android:layout_width="226dp"
        android:layout_height="24dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:paddingLeft="5dp"

        android:layout_marginTop="12dp"
        android:background="@color/colorPrimary"
        android:inputType="number"
        android:hint="field size"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/custom_name_in_reg" />

    <TextView
        android:id="@+id/textView_price_custom_reg"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="64dp"
        android:layout_marginRight="64dp"
        android:layout_marginTop="16dp"
        android:text="@string/price"
        android:textColor="@color/colorPrimaryDark"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/textView_price_in_custom_reg"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/textView_size_custom_reg"
        app:layout_constraintTop_toBottomOf="@+id/textView_size_custom_reg" />

    <EditText
        android:id="@+id/textView_price_in_custom_reg"
        android:paddingLeft="5dp"
        android:layout_width="226dp"
        android:layout_height="24dp"
        android:layout_marginTop="8dp"
        android:inputType="number"
        android:hint="price/hr"
        android:background="@color/colorPrimary"
        app:layout_constraintEnd_toEndOf="@+id/textView_size_in_reg_custom"
        app:layout_constraintTop_toBottomOf="@+id/textView_size_in_reg_custom" />

    <TextView
        android:id="@+id/textView_available_day_custom_reg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:text="@string/availability_days"
        android:textColor="@color/colorPrimaryDark"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="@+id/textView_price_custom_reg"
        app:layout_constraintTop_toBottomOf="@+id/textView_price_custom_reg" />

    <EditText
        android:id="@+id/textView_available_day_in_custom_reg"
        android:layout_width="226dp"
        android:layout_height="24dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorPrimary"
        android:hint="ex: (sun-wed)-mon"
        android:paddingLeft="5dp"
        app:layout_constraintEnd_toEndOf="@+id/textView_price_in_custom_reg"
        app:layout_constraintTop_toBottomOf="@+id/textView_price_in_custom_reg" />

    <TextView
        android:id="@+id/textView_available_time_custom_reg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:text="@string/availability_time"
        android:textColor="@color/colorPrimaryDark"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="@+id/textView_available_day_custom_reg"
        app:layout_constraintTop_toBottomOf="@+id/textView_available_day_custom_reg" />

    <EditText
        android:id="@+id/textView_available_time_in_custom_reg"
        android:layout_width="226dp"
        android:layout_height="21dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorPrimary"
        android:hint="ex:(8am-11pm)-(2pm-5pm)"
        android:paddingLeft="5dp"
        app:layout_constraintEnd_toEndOf="@+id/textView_available_day_in_custom_reg"
        app:layout_constraintTop_toBottomOf="@+id/textView_available_day_in_custom_reg" />

    <TextView
        android:id="@+id/textView_to_be_filled_custom_reg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:textColor="@color/colorPrimaryDark"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/custom_name_reg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="72dp"
        android:layout_marginRight="72dp"
        android:layout_marginTop="72dp"
        android:text="@string/name"
        android:textColor="@color/colorPrimaryDark"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/custom_name_in_reg"
        app:layout_constraintTop_toBottomOf="@+id/imageView_field" />

    <EditText
        android:id="@+id/custom_name_in_reg"
        android:layout_width="226dp"
        android:layout_height="20dp"
        android:paddingLeft="5dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="24dp"
        android:background="@color/colorPrimary"
        android:ems="10"
        android:hint="field name /number"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/uploud_field_pic_btn" />
</android.support.constraint.ConstraintLayout>

我动态生成此自定义约束视图,在其活动类的线性视图父级中多次线性生成,如下所示:

    for (int i = 0; i < f; i++) {

        // Add the text layout to the parent layout
        view = layoutInflater.inflate(R.layout.custom_owner_register_layout, parentLayout, false);

        // In order to get the view we have to use the new view with text_layout in it
        ConstraintLayout constraintLayout = (ConstraintLayout) view.findViewById(R.id.child);

        TextView textView = (TextView) view.findViewById(R.id.textView_to_be_filled_custom_reg);
        textView.setText("Field # " + (i + 1));

        // Add the text view to the parent layout
        parentLayout.addView(constraintLayout);
    }

现在我想从具有(编辑文本和图像)的每个约束子视图中设置并获取数据,我知道如何访问父级的子级,但是我不知道如何访问视图(编辑文本)和图像)的每个子组。

2 个答案:

答案 0 :(得分:1)

创建自定义视图持有者类ItemViewHolder extends RecyclerView.ViewHolder,然后将其设置为每个展开视图view.setTag(viewHolder);的标签

ItemViewHolder viewHolder = new ItemViewHolder(constraintLayout);
constraintLayout.setTag(viewHolder);
parentLayout.addView(constraintLayout);

在取回时,请使用以下

View childView = parentLayout.getChildAt(position);
ItemViewHolder vHolder = (ItemViewHolder) childView.getTag();

这是自定义视图持有人的样子

protected class ItemViewHolder extends RecyclerView.ViewHolder {
    public ItemViewHolder(View itemView) {
       super(itemView);
       ButterKnife.bind(this, itemView);
    }
}

答案 1 :(得分:1)

也许简单的方法是将ConstraintLayout保存在列表中,以便更轻松地设置和获取数据。