首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当处理多个图像时,如何使ImageView变得更干净?ImageView阵列?

当处理多个图像时,如何使ImageView变得更干净?ImageView阵列?
EN

Stack Overflow用户
提问于 2018-05-22 10:06:56
回答 1查看 29关注 0票数 0

我使用jsoup和ImageView显示来自URL的20个图片。代码可以工作,但是我想要一个关于如何使重复变量的混乱变得更干净的第二个观点。

代码语言:javascript
复制
package edu.learner.yaarpatandar.test;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    final StringBuilder albumArtIMG = new StringBuilder();
    int pageNum = 1;
    private Button nextPage;
    private Button backPage;

    private ImageView albumArt;
    private ImageView albumArt2;
    private ImageView albumArt3;
    private ImageView albumArt4;
    private ImageView albumArt5;

    private ImageView albumArt6;
    private ImageView albumArt7;
    private ImageView albumArt8;
    private ImageView albumArt9;
    private ImageView albumArt10;

    private ImageView albumArt11;
    private ImageView albumArt12;
    private ImageView albumArt13;
    private ImageView albumArt14;
    private ImageView albumArt15;

    private ImageView albumArt16;
    private ImageView albumArt17;
    private ImageView albumArt18;
    private ImageView albumArt19;
    private ImageView albumArt20;

    //String webURL = "";
    private String[] imgURL = new String[20];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        albumArt   = (ImageView) findViewById(R.id.imageView);
        albumArt2  = (ImageView) findViewById(R.id.imageView2);
        albumArt3  = (ImageView) findViewById(R.id.imageView3);
        albumArt4  = (ImageView) findViewById(R.id.imageView4);
        albumArt5  = (ImageView) findViewById(R.id.imageView5);
        albumArt6  = (ImageView) findViewById(R.id.imageView6);
        albumArt7  = (ImageView) findViewById(R.id.imageView7);
        albumArt8  = (ImageView) findViewById(R.id.imageView8);
        albumArt9  = (ImageView) findViewById(R.id.imageView9);
        albumArt10 = (ImageView) findViewById(R.id.imageView10);
        albumArt11 = (ImageView) findViewById(R.id.imageView11);
        albumArt12 = (ImageView) findViewById(R.id.imageView12);
        albumArt13 = (ImageView) findViewById(R.id.imageView13);
        albumArt14 = (ImageView) findViewById(R.id.imageView14);
        albumArt15 = (ImageView) findViewById(R.id.imageView15);
        albumArt16 = (ImageView) findViewById(R.id.imageView16);
        albumArt17 = (ImageView) findViewById(R.id.imageView17);
        albumArt18 = (ImageView) findViewById(R.id.imageView18);
        albumArt19 = (ImageView) findViewById(R.id.imageView19);
        albumArt20 = (ImageView) findViewById(R.id.imageView20);

        nextPage = (Button) findViewById(R.id.nextPage_button);
        backPage = (Button) findViewById(R.id.backPage_button);
        checkPage();
        getWebsite();

        nextPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pageNum++;
                checkPage();
                getWebsite();
            }
        });

        backPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pageNum--;
                checkPage();
                getWebsite();
            }
        });
    }

    private void getWebsite() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document doc = Jsoup.connect(SOME_URL + Integer.toString(pageNum)).get();
                    Elements imgLink = doc.select("img");
                    for(int i = 0; i < 20; i++) {
                        imgURL[i] = imgLink.get(i+1).attr("src");
                    }

                } catch (IOException e) {
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int width = 265, height = 265;

                        for(int i = 0; i < 20; i++) {
                            imgURL[i] = imgURL[i].replaceAll(" ", "%20");
                        }

                        Picasso.get().load(imgURL[0]).resize(width,height).into(albumArt);
                        Picasso.get().load(imgURL[1]).resize(width,height).into(albumArt2);
                        Picasso.get().load(imgURL[2]).resize(width,height).into(albumArt3);
                        Picasso.get().load(imgURL[3]).resize(width,height).into(albumArt4);
                        Picasso.get().load(imgURL[4]).resize(width,height).into(albumArt5);

                        Picasso.get().load(imgURL[5]).resize(width,height).into(albumArt6);
                        Picasso.get().load(imgURL[6]).resize(width,height).into(albumArt7);
                        Picasso.get().load(imgURL[7]).resize(width,height).into(albumArt8);
                        Picasso.get().load(imgURL[8]).resize(width,height).into(albumArt9);
                        Picasso.get().load(imgURL[9]).resize(width,height).into(albumArt10);

                        Picasso.get().load(imgURL[10]).resize(width,height).into(albumArt11);
                        Picasso.get().load(imgURL[11]).resize(width,height).into(albumArt12);
                        Picasso.get().load(imgURL[12]).resize(width,height).into(albumArt13);
                        Picasso.get().load(imgURL[13]).resize(width,height).into(albumArt14);
                        Picasso.get().load(imgURL[14]).resize(width,height).into(albumArt15);

                        Picasso.get().load(imgURL[15]).resize(width,height).into(albumArt16);
                        Picasso.get().load(imgURL[16]).resize(width,height).into(albumArt17);
                        Picasso.get().load(imgURL[17]).resize(width,height).into(albumArt18);
                        Picasso.get().load(imgURL[18]).resize(width,height).into(albumArt19);
                        Picasso.get().load(imgURL[19]).resize(width,height).into(albumArt20);
                    }
                });
            }
        }).start();
    }
    void checkPage(){
        if(pageNum == 1){
            backPage.setEnabled(false);
        }
        else{
            backPage.setEnabled(true);
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-22 10:15:38

您将不得不使用回收视图和加载此列表的图像使用适配器。您可以查看此链接以获得一些参考https://www.101apps.co.za/index.php/articles/android-recyclerview-and-picasso-tutorial.html

如果您需要更多帮助,请回发一些代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50465002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档