如何从URL列表填充ImageSwitcher? (编辑:卧式RecyclerView) [英] How can I populate an ImageSwitcher from a url list? (EDIT: Horizontal RecyclerView)
问题描述
我有一个包含字符串列表的对象.这些是图片的网址,我正在使用Picasso库在ImageView中进行设置.我想在ImageSwitcher中设置这些图像,但我不知道该怎么做(我之前做过一些研究,但没有结果).感谢您的帮助!
I have an object which includes a list of strings. These are urls to pictures and I'm using Picasso library to set in ImageView. I would like to set these images in an ImageSwitcher, and I dont know how to do it (I've done some researches before but no results). Thanks for helping guys !
我最终选择了卧式RecyclerView使其具有图库,因此在我的布局中只有一个ImageView.我现在也有一个回收站适配器,但是我不知道我是否必须在适配器中包括字符串列表,或者只是处理imageView并动态创建ImageView并将其添加到我的java类的回收站列表中....谢谢为帮助!
EDIT : I finally chose a Horizontal RecyclerView to have a Gallery like, so in my layout for a row there's only an ImageView. I also have now a Recycler Adapter but i dont know if i have to include my list of strings in the adapter, or just handle the imageView and create dynamically an ImageView and add it to the recycler list in my java class.... Thanks for helping !
推荐答案
您的Adapter
应该具有一个构造函数,在该构造函数中,您会将url作为参数传递给Adapter
.您的Adapter
看起来像这样:
Your Adapter
should have a constructor in which you will pass the urls as a parameter to the Adapter
. Your Adapter
will look something like this:
public class ImageSwitcherAdapter extends RecyclerView.Adapter<ImageSwitcherAdapter.MyViewHolder> {
private Context context;
private List<String> urls;
public ImageSwitcherAdapter(Context context, List<String> urls) {
this.context = context;
this.urls = urls;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.your_row_layout, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Uri uri = Uri.parse(urls.get(position));
Picasso.with(context).load(uri).into(holder.image);
}
@Override
public int getItemCount() {
return urls.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.your_imageview);
}
}
}
并将Adapter
设置为RecyclerView
会看起来像这样:
And setting the Adapter
to your RecyclerView
will look something like this:
RecyclerView recyclerView = findViewById(R.id.your_recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
ImageSwitcherAdapter adapter = new ImageSwitcherAdapter(this, urls);
recyclerView.setAdapter(adapter);
这篇关于如何从URL列表填充ImageSwitcher? (编辑:卧式RecyclerView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!