Skip to content Skip to sidebar Skip to footer

How Can I Populate An Imageswitcher From A Url List? (edit: Horizontal Recyclerview)

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 ImageSwitche

Solution 1:

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:

publicclassImageSwitcherAdapterextendsRecyclerView.Adapter<ImageSwitcherAdapter.MyViewHolder> {

    private Context context;
    private List<String> urls;

    publicImageSwitcherAdapter(Context context, List<String> urls) {
        this.context = context;
        this.urls = urls;
    }

    @Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Viewview= LayoutInflater.from(context).inflate(R.layout.your_row_layout, parent, false);
        returnnewMyViewHolder(view);
    }

    @OverridepublicvoidonBindViewHolder(MyViewHolder holder, int position) {
        Uriuri= Uri.parse(urls.get(position));
        Picasso.with(context).load(uri).into(holder.image);
    }

    @OverridepublicintgetItemCount() {
        return urls.size();
    }

    publicstaticclassMyViewHolderextendsRecyclerView.ViewHolder {
        private ImageView image;

        publicMyViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.your_imageview);
        }
    }
}

And setting the Adapter to your RecyclerView will look something like this:

RecyclerViewrecyclerView= findViewById(R.id.your_recyclerview);

LinearLayoutManagerlayoutManager=newLinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);

ImageSwitcherAdapteradapter=newImageSwitcherAdapter(this, urls);
recyclerView.setAdapter(adapter);

Post a Comment for "How Can I Populate An Imageswitcher From A Url List? (edit: Horizontal Recyclerview)"