Picasso Library and GridView Images

前端 未结 1 1483
挽巷
挽巷 2021-01-12 17:26

I want to build an app that displays images in a gridview by using the picasso library. The images are retrieved by a remote server. Should I make an AsyncTask class or is t

1条回答
  •  生来不讨喜
    2021-01-12 18:02

    Its very simple to use picasso lib for loading images in gridview, as demonstrated here,

    class SampleGridViewAdapter extends BaseAdapter {
      private final Context context;
      private final List urls = new ArrayList();
    
      public SampleGridViewAdapter(Context context) {
        this.context = context;
    
        // Ensure we get a different ordering of images on each run.
        Collections.addAll(urls, Data.URLS);
        Collections.shuffle(urls);
    
        // Triple up the list.
        ArrayList copy = new ArrayList(urls);
        urls.addAll(copy);
        urls.addAll(copy);
      }
    
      @Override public View getView(int position, View convertView, ViewGroup parent) {
        SquaredImageView view = (SquaredImageView) convertView;
        if (view == null) {
          view = new SquaredImageView(context);
          view.setScaleType(CENTER_CROP);
        }
    
        // Get the image URL for the current position.
        String url = getItem(position);
    
        // Trigger the download of the URL asynchronously into the image view.
        Picasso.with(context) //
            .load(url) //
            .placeholder(R.drawable.placeholder) //
            .error(R.drawable.error) //
            .fit() //
            .tag(context) //
            .into(view);
    
        return view;
      }
    
      @Override public int getCount() {
        return urls.size();
      }
    
      @Override public String getItem(int position) {
        return urls.get(position);
      }
    
      @Override public long getItemId(int position) {
        return position;
      }
    }
    

    0 讨论(0)
提交回复
热议问题