Home > Software engineering >  android - Imageview has height 0 in adapter
android - Imageview has height 0 in adapter

Time:07-25

I've an imageview in my RecyclerView's adapter ... I download the image for it with Glide library . this is item view code :

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="8dp"
app:cardElevation="5dp"
card_view:cardCornerRadius="8dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@ id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true" />

    <TextView
        android:id="@ id/onvan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:padding="3dp"
        android:textColor="#000"
        android:textSize="17sp"
        android:textStyle="bold" />

.....

this is my adapter class :

 @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parrent, int i) {
        View view = inflater.inflate(R.layout.blog_row, parrent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

 @Override
    public void onBindViewHolder(MyViewHolder viewHolder, int position) {
        Blog_items item = list.get(position);
        Glide.with(ctx).load(ctx.getString(R.string.url)   "NewsPictures/"   imgurl).into(viewHolder.img);

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView title, rateNum, tv_blog_dates, shortTozihat;
        ImageView img;
        RatingBar rate;

        public MyViewHolder(View itemView) {
            super(itemView);
            img =itemView.findViewById(R.id.img);
        }

it creates the views and I've 10 rows .. each row has it's own image and the image URL is 100percent true I'm sure about it but it has 0 height and it doesn't shown . if I set the imageview's height manually , it'll show

what is wrong ? I don't want to set it manually because I have different images with different heights

what is wrong with it ?

CodePudding user response:

You can override the imagesize after setting it as wrap content

Glide
    .with(context)
    .load(path)     
    .apply(new RequestOptions().override(600, 200))
    .centerCrop() 
    .into(imageViewResizeCenterCrop);

CodePudding user response:

You can try to load image as bitmap first with asBitmap() then attach that bitmap to your imageview in on onResourceReady.

Glide
.with(ctx)
.asBitmap()
.load(ctx.getString(R.string.url)   "NewsPictures/"   imgurl)
.into(object : CustomTarget<Bitmap>(){
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {               
                viewHolder.img.setImageBitmap(resource)
            }

            override fun onl oadCleared(placeholder: Drawable?) {
                //Ignore
            }

        });
  • Related