【问题标题】:Retrieve all of the images from the server via Glide or Volley library and show them in list view通过 Glide 或 Volley 库从服务器检索所有图像并在列表视图中显示它们
【发布时间】:2019-02-26 15:55:51
【问题描述】:

我想通过 Glide 或 Volley 库从服务器检索所有图像并在列表视图中显示它们。我该怎么做 ?

【问题讨论】:

    标签: android eclipse server


    【解决方案1】:

    在你的 Gradle 中

    repositories {
      mavenCentral()
      google()
    }
    
    dependencies {
      implementation 'com.github.bumptech.glide:glide:4.8.0'
      annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
    }
    

    将图片加载到列表中

      String url = myUrls.get(position);
    
      GlideApp
        .with(myFragment)
        .load(url)
        .centerCrop()
        .placeholder(R.drawable.loading_spinner)
        .into(myImageView);
    

    【讨论】:

      【解决方案2】:

      您不需要进行图像管理。 Glide 可以在内部为您完成。

      对于ListView,在适配器的getView()方法中使用glide加载图片

      对于Recyclerview,在AdapteronBindViewHolder()方法中使用glide加载图片。

      【讨论】:

        【解决方案3】:

        创建一个Extends ArrayAdapter<ImageView> 的java 类并将其用于ListView

        class imageadapter extends ArrayAdapter<ImageView> {
        
            private Context context;
            private LayoutInflater inflater;
        
            private String[] imageurls;
        
            imageadapter(Context context, String[] imageurls) {
                super(context, R.layout.imagelayout);
                this.imageurls = imageurls;
                this.context = context;
                this.inflater = LayoutInflater.from(context);
            }
        
        
            @SuppressLint({"ViewHolder", "SetTextI18n", "ClickableViewAccessibility"})
            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        
                View layout = convertView;
                Holder holder = null;
                if (layout == null) {
                    layout = inflater.inflate(R.layout.imagelayout, parent, false);
                    holder = new Holder();
                    holder.imageView1 = layout.findViewById(R.id.imageView);
                    holder.textView1 = layout.findViewById(R.id.textView);
                    layout.setTag(holder);
                } else {
                    holder = (Holder) layout.getTag();
                }
        
                Picasso.get().load(imageurls[position]).fit().into(holder.imageView1);
                holder.textView1.setText("Image:" + (position + 1));
        
                return layout;
            }
        
            @Override
            public int getCount() {
                return imageurls.length;
            }
        
        
            static class Holder {
        
                ImageView imageView1;
                TextView textView1;
        
            }
        
        }
        

        【讨论】:

          【解决方案4】:

          我想通过 Glide 或 凌空图书馆

          这些库实际上用于在 Android 上加载和缓存图像

          为了下载和显示所有这些图像,您可以使用json_encode()以 PHP 为例)然后使用@987654321 从服务器端生成所有这些链接的Json 输出@ 在 Android 中循环遍历链接的整个 Json 输出,然后通过 GlideVolley 库显示图像。

          编辑

          这是应该的,在RecyclerViewAdapter内部的Android(客户端)上:

          Glide.with(myContext)
                              .load(mPosts.imgurl)
                              .apply(options)
                              .into(ViewHolder.imgShow)
          

          关注:https://ledron.github.io/RecyclerView/

          但是,您可以在 Android 上的 ArrayList 中单独添加这些链接,然后也显示这些链接。按照教程使用 Adapter for RecyclerView

          例如Adapter:

          public class Adapter  extends RecyclerView.Adapter<Adapter.ViewHolder>{
          
              ArrayList<String> urls;
              Context context;
              //constructor
              public Adapter(ArrayList<String> ImgUrl, Context context_)
              {
                  this.urls = ImgUrl;
                  this.context = context_;
              }
          
              public static class ViewHolder extends RecyclerView.ViewHolder
              {
                  private ImageView image;
          
                  public ViewHolder(View v)
                  {
                    super(v);
                    image =(ImageView)v.findViewById(R.id.img);
                  }
          
                  public ImageView getImage(){ return this.image;}
              }
          
              @Override
              public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
              {
                  View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem, parent, false);
                  v.setLayoutParams(new RecyclerView.LayoutParams(1080,800));
                  return new ViewHolder(v);
              }
          
              @Override
              public void onBindViewHolder(final ViewHolder holder, int position)
              {
                  Glide.with(this.context)
                          .load(urls.get(position))
                          .diskCacheStrategy(DiskCacheStrategy.ALL)
                          .into(holder.getImage());
              }
          
              @Override
              public int getItemCount()
              {
                  return urls.size();
              }
          
          }
          

          【讨论】:

          • Tnx,如何使用 json_encode()(例如在 PHP 上)从服务器端生成所有这些链接的 JSON 输出?你能告诉我吗?
          • 刚才已经说过了。您可以在服务器端创建一个 URL 列表(使用 Google),然后使用 json_encode 提供 json 输出。这也和另一个问题有关,不属于这个问题。更多问题请使用“提问”。
          • 谢谢,但我是 android 的初学者,我知道我应该“在服务器端制作一个 URL 列表(使用 Google),然后使用 json_encode 提供 JSON 输出”,但是如何做吗?我想要它的代码...
          • 问这个问题是不是个好问题:“为了下载和显示所有图像,我想使用 json_encode() 从服务器端生成所有链接的 JSON 输出(在 PHP 上例子).tnx
          • @Mpi 您还可以在ArrayList 中添加这些链接,并在RecyclerView 中显示它们,而无需服务器端代码。但如您所知,这不是未来添加链接的推荐方式:) 检查更新的答案。
          猜你喜欢
          • 1970-01-01
          • 2013-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-18
          • 1970-01-01
          • 1970-01-01
          • 2012-09-22
          相关资源
          最近更新 更多