水平RecyclerView图像

时间:2017-11-23 11:41:38

标签: java android android-recyclerview imageview horizontal-scrolling

获取图像的Horizo​​ntal RecyclerView的代码是什么?我已经有了水平RecyclerView of Strings的代码。我需要有相同的图像。 我必须创建一个10水平可滚动imageView的RecyclerView。 这是我的10个水平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);
   } }
}

我以这种方式在Fragment上管理它:

        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"}));

我该如何修改?

3 个答案:

答案 0 :(得分:2)

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);
   } }
}

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}));
  

注意:您的布局(item_layout_recycler_view_top_10)需要包含ImageView而不是TextView

答案 1 :(得分:0)

您想要图片吗?

而不是一个充满textViews(R.layout.item_layout_recycler_view_top_10)的recyclerView

只需创建另一个包含ImageView而不是TextView的布局,并根据您当前的解决方案使用它。

答案 2 :(得分:0)

字符串数组更改为图像url数组或可绘制图像数组创建并发送到适配器并设置为imageview。