Imageview in a recyclerview

◇◆丶佛笑我妖孽 提交于 2020-01-06 05:51:21

问题


I have an imageview in row.xml and I set a picture for it in xml layout. But when I use that in adapter (onbindviewholder method) Not show any image . But when I set image resource in onbindviewholder that works and show the image !!! What is wrong in the first method ???


回答1:


use

holder.Image.setImageResource();

or you can use Glide or Picasso library to load images.

and also return the size of your list.

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



回答2:


public class Adapter extends RecyclerView.Adapter<Adapter.vh> {
    private List<Contacts> contacts;

    public Adapter(List<Contacts> contacts) {
        this.contacts = contacts;
    }

    public class vh extends RecyclerView.ViewHolder {
        protected TextView first_name;
        protected TextView last_name;
        protected ImageView Image;

        public vh(View v) {
            super(v);
            first_name = (TextView) v.findViewById(R.id.first);
            last_name = (TextView) v.findViewById(R.id.last);
            Image = (ImageView) v.findViewById(R.id.imageView);
        }
    }

    @Override
    public Adapter.vh onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
        return new vh(v);
    }

    @Override
    public void onBindViewHolder(vh holder, int position) {
        Contacts example = contacts.get(position);
        holder.first_name.setText(example.name);
        holder.last_name.setText(example.last_name);
        if (position == 3) {
            holder.first_name.setTextColor(Color.BLUE);
        }
    }

    @Override
    public int getItemCount() {
    }
}


来源:https://stackoverflow.com/questions/46262900/imageview-in-a-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!