androidxmlandroid-layoutandroid-viewgroup

How to replicate a ViewGroup n times in another ViewGroup but with different values for Views?


I am working on an Android application in which I have to create a card with multiple TextView and ImageView inside it. But these views are replicated multiple times in the card with different images and text (The attached picture summarizes my expected result better). I want to assign different images and text values to these views programmatically.

One solution is to use any layout and populate it with these Views, but it has too much code repetition. Another way is to create a vertical LinearLayout for the TextView, ImageView, TextView combination, and use <include> inside my parent Card. But how can I access the child Views by ID using this method to manipulate them later programmatically. What is the standard way of designing such a layout with minimum repetition of code? I hope there is a better way out. Here is the code for my LinearLayout.

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/margin_parent">

    <TextView
            android:id="@+id/textview_whatever_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"/>

    <ImageView
            android:id="@+id/imageview_whatever"
            android:layout_width="@dimen/icon_height"
            android:layout_height="@dimen/icon_height"
            android:layout_marginBottom="4dp"/>

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

</LinearLayout>

Here is the result I expect to create. Expected result


Solution

  • I think the best way would be to use the Include way, give the Includes an ID

    <include
    android:id="@+id/whatever1"
    

    You can refer to the items by

    whatever1.textview_whatever_1
    

    etc.