简体   繁体   中英

Imageview in a recyclerview

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 ???

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM