简体   繁体   中英

How to create 'i' number of objects in a layout (by looping)?

I need to create i number of photo_view and give it a special amount every time (loop). number of photo_view in every time will be different. What exactly should I do in the code ?

my xml file :

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:custom="http://schemas.android.com/tools"
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/black">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <com.github.chrisbanes.photoview.PhotoView
                    android:id="@+id/photo_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
            </LinearLayout>
        </HorizontalScrollView>

        <ImageView
            android:id="@+id/exit_btn"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_margin="20dp"
            android:clickable="true"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_back_ltr" />

    </android.support.design.widget.CoordinatorLayout>

Dynamically adding Views to a ViewGroup is what you are looking for. which is done by code not by XML.

In general, there are two ways to add a view dynamically, either by inflating some layout file in a view then add this view to your parent viewgroup ( LinearLayout for ex) by calling the add method on this parent view.

Or by defining a new object of that view and add all params needed to it then add it to the parent viewgroup .

See an example about way 1 here

See an example about way 2 here


in your case you could create a new layout.xml file contains only your custom view com.github.chrisbanes.photoview.PhotoView with all the needed params. then add an id for the parent linearlayout

        <LinearLayout
            android:"@+id/parentViewGroup"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">


        </LinearLayout>

And in your code, loop by the count and add the custom inflated view to parent. like this:

View inflatedView = View.inflate(context, yourViewXML, null);
LinearLayout parentViewGroup = findViewById(R.id.parentViewGroup);

for(int i = 0 ; i<count ; i++){
  parentViewGroup.add(inflatedView);
}

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