How to get recyclerView's item height during onBindViewHolder ?

HSTan.0019HSTan.0019 USMember
edited October 2015 in Xamarin.Android
public override void OnBindViewHolder (RecyclerView.ViewHolder holder, int position)
{
    LatestNewsHolder viewHolder = holder as LatestNewsHolder;
    viewHolder.position = position;

Console.WriteLine(viewHolder.mImage.Height + "");   //  I tried to invoke here but it keep returns 0
}

public class LatestNewsHolder : RecyclerView.ViewHolder
{
public Context context { get; set; }
public View mMainView { get; set; }
public ImageView mImage { get; set; }

...
}

In my XML, my ImageView is set to match_parent (width) & wrap_content (height).

Answers

  • BerayBentesenBerayBentesen TRUniversity ✭✭✭✭

    What is your purpose with getting height ?

  • HSTan.0019HSTan.0019 USMember
    edited October 2015

    I'm trying to decode and set image from my local device during OnBindViewHolder ()

    BitmapFactory.Options options = new BitmapFactory.Options ();
    options.InJustDecodeBounds = true;
    await BitmapFactory.DecodeFileAsync (_imagePath, options);
    options.InSampleSize = options.OutWidth > options.OutHeight ? options.OutHeight / **imageViewHeight** : options.OutWidth / **imageViewWidth**;
    options.InJustDecodeBounds = false;
    Bitmap bitmap = await BitmapFactory.DecodeFileAsync (_imagePath, options);
    imageView.SetImageBitmap (bitmap);
    

    or is there any better way to implement this ?

  • BerayBentesenBerayBentesen TRUniversity ✭✭✭✭

    First define your image view in RecyclerView.ViewHolder like this :

    public ImageView _propertyphoto {get;set;}

    Then re-define in OnCreateViewHolder :

    var PropertyImage = row.FindViewById (Resource.Id.PropertyImage);

    var view = new PropertiesRView (row, OnClick)
    {
    _propertyphoto = PPropertyImage,
    };

    return view;

    And you can set source in OnBindViewHolder like this :

    var item = propertieslist [position];

    Koush.UrlImageViewHelper.SetUrlDrawable (ContactsHolder._propertyphoto, item.PropertyImageUrl,
    Resource.Drawable.adresinempty); ( I used library for multiple image download )

    Also today I'll be writing post about RecyclerView with sample project. I'll make you notified after publishing.

Sign In or Register to comment.