如何在Android上将List <string>设置为适用于ImageView的RecyclerView适配器

时间:2018-03-05 08:44:21

标签: java android android-recyclerview

在我的应用程序中,我希望将图像展示到RecyclerView 适配器 我在下面编写代码,但没有向ImageView显示任何图像。

我的适配器代码:

public class DetailMiniGalleryAdapter extends RecyclerView.Adapter<DetailMiniGalleryAdapter.ViewHolder> {
    private List<String> model;
    private Context context;

    public DetailMiniGalleryAdapter(Context context, List<String> model) {
        this.context = context;
        this.model = model;
    }

    @Override
    public DetailMiniGalleryAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row_detail_mini_gallery, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DetailMiniGalleryAdapter.ViewHolder viewHolder, int i) {

        Glide.with(context)
                .load(Constants.SERVER + model)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(viewHolder.listRow_detailMiniGalleryImg);
        Log.e("galleryImages", model+"");
    }

    @Override
    public int getItemCount() {
        return model.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView listRow_detailMiniGalleryImg;

        public ViewHolder(View view) {
            super(view);

            listRow_detailMiniGalleryImg = view.findViewById(R.id.listRow_detailMiniGalleryImg);
        }
    }
}

我的图片网址在LogCat中:

/galleryImages: [/img/ps/1.png, /img/ps/2.png, /img/ps/3.png, /img/ps/4.png, /img/ps/5.png]

我在mainActivity中使用此代码填充适配器:

miniGalleryAdapter = new DetailMiniGalleryAdapter(getActivity(), detail.getImage());

但是当使用此代码时:.load(Constants.SERVER + model.get(0))会向我展示图片,但只显示 1image !我想要显示所有图像

我怎么能?

1 个答案:

答案 0 :(得分:1)

不要直接在加载中使用model.get(i),而是使用 Glide.with(context) .load(Constants.SERVER + model.get(i)) //< i is position of item .diskCacheStrategy(DiskCacheStrategy.SOURCE) .into(viewHolder.listRow_detailMiniGalleryImg);

IServiceProvider