如何在不同的imageView上设置List <>的图像src的倍数?

时间:2018-07-25 05:59:21

标签: android for-loop arraylist

我将来自Firestore的图像URL存储到ArrayList,并且成功将所有图像URL存储到ArrayList。之后,我尝试通过for loop将其取出,并且在内部循环中将始终循环图像URL,以及如何获取循环图像URL值并将其放置在其他imageView上?

以下是我的代码:

private List<String>imgUrl= new ArrayList<>();
private ImageView imageview1, imageview2, imageview3,imageview4,imageview5;


if(imgUrl!= null){  // size : 5
        for(int i= 0; i<imgUrl.size(); i ++){ //loop 
            //how I can loop the imageUrl here into different ImageView?
        }
    }

6 个答案:

答案 0 :(得分:1)

您可以使用ImageViews数组

private List<String> imgUrl= new ArrayList<>();
private ImageView imageview1, imageview2, imageview3,imageview4,imageview5;

private ImageView[] imageViewArray = new ImageView[]{imageview1, imageview2, imageview3,imageview4,imageview5};

if(imgUrl!= null){  // size : 5
    for(int i= 0; i<imgUrl.size(); i ++){ //loop
        //how I can loop the imageUrl here into different ImageView?
        Picasso.get().load(imgUrl.get(i)).into(imageViewArray[i]);
    }
}

答案 1 :(得分:1)

private ArrayList<ImageView> images = new ArrayList();
images.add(imageview1);
images.add(imageview2);
images.add(imageview3);
images.add(imageview4);
images.add(imageview5);

int cnt = 0;
for (String url : imgUrl) {
    cnt++;
    ImageView img = images.get(cnt);

}

答案 2 :(得分:1)

如果需要,您也可以创建自定义xml布局文件。如果要在图像下方添加描述,这将更加容易。 对于下面的示例,我正在使用Glide设置图像

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout container = findViewById(R.id.container);

    LayoutInflater layoutInflater = (LayoutInflater) parentActivity.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View newView = layoutInflater.inflate(R.layout.many_image_views, null);

    if(newView != null){

        ImageView anImageView = newView.findViewById(R.id.anImageView);

        for(int i = 0; i < imgUrl.size(); i++){
            GlideApp.with(this)
                    .asBitmap().format(PREFER_RGB_565)
                    .load(imgUrl.get(i))
                    .placeholder(R.drawable.ic_loading_image)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .error(R.drawable.ic_not_found)
                    .into(anImageView);

            container.addView(newView);
        }
    }
}

many_image_views.xml

<?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="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clickable="true"
android:focusable="true">

<ImageView
    android:id="@+id/anImageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

</LinearLayout>

答案 3 :(得分:0)

如果将这些ImageViews放入XML中,则可以使用Picasso或其他将URL加载到图像中的方法,您可以执行以下操作-

  

您需要首先参考所有这些图像。

 private List<String> imgUrl = new ArrayList<>();
    private List<ImageView> imageViews = new ArrayList<>();
    private ImageView imageview1, imageview2, imageview3, imageview4, imageview5;

    imageViews.add(imageview1);
    imageViews.add(imageview2);
    imageViews.add(imageview3);
    imageViews.add(imageview4);
    imageViews.add(imageview5);

    if (imgUrl != null) {  
        for(int i=0;i<imgUrl.size();i++){
            Picasso.get().load(imgUrl.get(i)).into(imageViews.get(i));
        }
    }

,如果Image Url的大小小于ImageViews的5,则要避免ArrayIndexOutOfBondException,可以compare URLImages两种大小来做到这一点-

   if (imgUrl != null) {

    int size = (imgUrl.size==imageViews.size() ? imgUrl.size() :imgUrl.size<imageViews.size() ?imgUrl.size() :imageViews.size();
                for(int i=0;i<size;i++){
                    Picasso.get().load(imgUrl.get(i)).into(imageViews.get(i));
                }
            }

或者,如果要动态创建这些图像,然后将其放大到视图中,则可以在for循环中完成所有这些操作。

答案 4 :(得分:0)

您可以将图像视图放置在如下数组中:

private List<ImageView> imageViews = new ArrayList<>();

然后遍历它并加载图像:

if(imgUrl!= null){  // size : 5
        for(int i= 0; i<imgUrl.size(); i ++){ //loop 
            Picasso.with(this).load(imgUrl.get(i)).into(imageViews.get(i));
        }
    }

答案 5 :(得分:0)

尝试此代码

private List<String>imgUrl= new ArrayList<>();
private ImageView imageview1, imageview2, imageview3,imageview4,imageview5;
Private View view;

if(imgUrl!= null){  // size : 5
    for(int i= 0; i<imgUrl.size(); i ++){ //loop 
        if(i==0){
           view=imageview1;
         }
        else if(i==1){
           view=imageview2;
         }
        Picasso.get().load(imgUrl.get(i).into(view);
    }
}