Picasso/Glide 从 youtube 搜索结果中仅加载 6 个缩略图

Picasso/Glide loads only 6 thumbnails from youtube search result

我正在尝试将 YouTube 搜索结果中的缩略图加载到列表视图中。但是我只得到前 6 个缩略图,如果我使用 Picasso,其余的 imageView 将填充黑色方块,如果我使用 Glide,则填充白色方块。

这是我的自定义阵列适配器:

        class MyListAdapter extends ArrayAdapter<Item>{
            public MyListAdapter(){
                super(SearchResult.this, R.layout.item_view, myItems);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View itemView = convertView;
                if (itemView==null){
                    itemView = getLayoutInflater().inflate(R.layout.item_view,parent,false);
                }

                Item currentItem = myItems.get(position);
                TextView title = (TextView) itemView.findViewById(R.id.tvTitle);
                title.setText(currentItem.getTitle());

                TextView uploader = (TextView) itemView.findViewById(R.id.tvUploader);
                uploader.setText(currentItem.getUploader());

                TextView timeNView = (TextView) itemView.findViewById(R.id.tvTimeNView);
                timeNView.setText(currentItem.getTimeNView());

                TextView duration = (TextView) itemView.findViewById(R.id.tvDuration);
                duration.setText(currentItem.getDuration());

                ImageView thumb = (ImageView) itemView.findViewById(R.id.imageView);


                Picasso.with(SearchResult.this).load(currentItem.getImageURL()).placeholder(R.drawable.placeholder).into(thumb);

                return itemView;
            }
        }

和 ImageView:

    <ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

"black" 视图出现是因为 Picasso 收到空 url(阅读下面的评论以进一步了解发生这种情况的原因)。

以下是我建议的代码,包括结构更改和问题修复(最终由 Eisenheim 自己找到):

1。我没有扩展 ArrayAdapter,而是扩展了 BaseAdapter
2Context 现在传递给适配器的构造函数
3LayoutInflater 在构造函数中定义一次
4。静态 Holder 持有对视图

的引用(R.id.imageViewR.id.tvDuration ...)
public class MyAdapter extends BaseAdapter {

    Context context;
    LayoutInflater inflater;
    ArrayList<Item> myItems;

    public MyAdapter(Context context, ArrayList<Item> myItems) {
        this.context = context;
        this.myItems = myItems;

        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return myItems.size();
    }

    @Override
    public Object getItem(int position) {
        return myItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0; //'0' is the default return value, you may change this if necessary
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Holder holder;
        Item currentItem = (Item) getItem(position);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_view, null);
            holder = new Holder();

            holder.title = (TextView) convertView.findViewById(R.id.tvTitle);
            holder.uploader = (TextView) convertView.findViewById(R.id.tvUploader);
            holder.timeNView = (TextView) convertView.findViewById(R.id.tvTimeNView);
            holder.duration = (TextView) convertView.findViewById(R.id.tvDuration);
            holder.thumb = (ImageView) convertView.findViewById(R.id.imageView);

            convertView.setTag(holder);

        } else {
            holder = (Holder) convertView.getTag();
        }

        holder.title.setText(currentItem.getTitle());
        holder.uploader.setText(currentItem.getUploader());
        holder.timeNView.setText(currentItem.getTimeNView());
        holder.duration.setText(currentItem.getDuration());

        Picasso.with(context).load("http://img.youtube.com/vi/" + 
                currentItem.getVideoId(‌​) + "/default.jpg").
                placeholder(R.drawable.placeholder).into(holder.thumb);

        return convertView;
    }

    static class Holder {
        TextView title, uploader, timeNView, duration;
        ImageView thumb;
    }
}

我推荐你使用 RecyclerView,因为它减少了系统的负载,因此你有更多的处理和内存来缓存更多的图像。确保您的图像不是太大或使用 Picasso 或 Glide 中的选项将它们裁剪到您需要的大小,另外如前一个答案所说,调试以查看您没有设置空 URL。

亲切的问候!