Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to set Tag for a view inside GetCellCore of CellRenderer ?

$
0
0

I have a Custom CellRenderer ( Android ) for list items in a Xamarin.Forms.ListView.

I am trying to create a custom ViewHolder pattern , so that I can get rid of unnecessary view.FindViewById calls.

Here is my code :

`[assembly: ExportRenderer (typeof (CommentsListItem), typeof (CommentsListItemCellRenderer))]
namespace TCMobile.Droid
{
public class ViewHolder : Java.Lang.Object{
public ImageView commentDeleteImage;
public TextView commentDescription;
public TextView commentAuthor;
public TextView commentModified;
}

public class CommentsListItemCellRenderer : CellRenderer
{

    Android.Views.View view;
    Activity activity = null;
    ViewHolder holder = null;

    private bool assignDelegate = false;



    public CommentsListItemCellRenderer ()
    {
    }

    protected override Android.Views.View GetCellCore (Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context)
    {
        base.GetCellCore (item, convertView, parent, context);
        assignDelegate = false;

        view = convertView;
        var x = (CommentsListItem)item;

        activity = context as Activity;
        if (view == null) {// no view to re-use, create new
            assignDelegate = true;
            view = activity.LayoutInflater.Inflate (Resource.Layout.TodoCommentsListCell, null);

            holder = new ViewHolder ();
            holder.commentDeleteImage = view.FindViewById<ImageView> (Resource.Id.comments_delete_image);
            holder.commentDescription = view.FindViewById<TextView> (Resource.Id.comments_text); 
            holder.commentAuthor = view.FindViewById<TextView> (Resource.Id.comments_username); 
            holder.commentModified = view.FindViewById<TextView> (Resource.Id.comments_modified);

            view.Tag = holder;

        } else {
            // PROBLEM IN THIS LINE
            holder = (ViewHolder) view.Tag;
        }

        FontHelper.getFontHelper().applyFont(holder.commentAuthor, Constants.FONT_OPEN_SANS_BOLD);
        FontHelper.getFontHelper().applyFont(holder.commentDescription, Constants.FONT_OPEN_SANS_REGULAR);
        FontHelper.getFontHelper().applyFont(holder.commentModified, Constants.FONT_OPEN_SANS_REGULAR);

        holder.commentDescription.Text = x.CommentsText;
        holder.commentAuthor.Text = x.AuthorName;
        holder.commentModified.Text = x.CreatedDate;

        if (x.EditEnabled) {
            holder.commentDeleteImage.Visibility = Android.Views.ViewStates.Visible;
        } else {
            holder.commentDeleteImage.Visibility = Android.Views.ViewStates.Gone;
        }

        if (assignDelegate) {
            holder.commentDeleteImage.Click += delegate {
                if (x.EditEnabled) {
                    MessagingCenter.Send<CommentsListItemCellRenderer, string> (this, "DeleteComment", x.CommentId);
                }
            };
        }

        return view;
    }



}

}
`

The problem is , in case of reused views i.e. when convertView is not null , I am getting an InvalidCastException in the line ,
holder = (ViewHolder) view.Tag;

I have also tried holder = view.Tag as ViewHolder .
But it returned null value .
Also, I do not know how the view.SetTag(int, Object) method can be useful in this case.

Is there any other way to do this , within the Cell Renderer ?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>