Horizontal RecyclerView of images

Viewed 4598

Which is the code to obtain a Horizontal RecyclerView of images? I already have the code for horizontal RecyclerView of Strings. I need to have the same but of Images. I have to create a RecyclerView of 10 horizontal scrollable imageView. This is my actual code of 10 horizontal TextViews:

public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder> {

private String[] items;

public HorizontalAdapter(String[] items) {

    this.items = items;

}

@Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.item_layout_recycler_view_top_10, parent, false);
    return new HorizontalViewHolder(view);
}

@Override
public void onBindViewHolder(HorizontalViewHolder holder, int position) {
    holder.text.setText(items[position]);
}

@Override
public int getItemCount() {
    return items.length;
}

public class HorizontalViewHolder extends RecyclerView.ViewHolder {
    TextView text;

    public HorizontalViewHolder(View itemView) {
        super(itemView);
        text = itemView.findViewById(R.id.text_view_top_10_ricette_titolo_ricetta);
   } }
}

and I manage it on a Fragment in this way:

        RecyclerView list = (RecyclerView) view.findViewById(R.id.recycler_view_ricette_categorie_primi_top_10);
    list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
    list.setAdapter(new HorizontalAdapter(new String[]{"Android","Programming","Java","RecyclerView","Layout"}));

How I have to modify this?

3 Answers

Try this make below changes in your HorizontalAdapter

public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder> {

private int[] items;

public HorizontalAdapter(String[] int) {

    this.items = items;

}

@Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.item_layout_recycler_view_top_10, parent, false);
    return new HorizontalViewHolder(view);
}

@Override
public void onBindViewHolder(HorizontalViewHolder holder, int position) {
    holder.image.setImageResource(items[position]);
}

@Override
public int getItemCount() {
    return items.length;
}

public class HorizontalViewHolder extends RecyclerView.ViewHolder {
    ImageView image;

    public HorizontalViewHolder(View itemView) {
        super(itemView);
        image = itemView.findViewById(R.id.imageviewid);
   } }
}

code for RecyclerView

RecyclerView list = (RecyclerView) view.findViewById(R.id.recycler_view_ricette_categorie_primi_top_10);
    list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
    list.setAdapter(new HorizontalAdapter(new int[]{R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher}));

NOTE : Your layout (item_layout_recycler_view_top_10) needs to contain a ImageView instead of a TextView

Instead of a recyclerView full of textViews (R.layout.item_layout_recycler_view_top_10) you want images?

Just create another layout which contains an ImageView instead of a TextView in it and use it accordingly to your current solution.

String array change to image url array or drawable image array create and send to adapter and set into imageview.

Related