ListView Multiple click in row item

scubby3scubby3 Member ✭✭
edited June 2018 in Xamarin.Android

I am looking for help with this problem that i am facing when i set up click events on item rows inside of a list view.
I have been working with two adapter switching them around to see which works better. however both seem to have its own problem.

on the first one listed below i am using binding and it does what i want but when i am scrolling it stick or freeze for a quick second before came into view.

first one

using System;
using System.Collections.Generic;

using Android.App;
using Android.Content;
using Android.Views;
using Android.Widget;
using Com.Bumptech.Glide;

namespace FunStock
{
    class FeedAdapter : BaseAdapter<Feed>
    {
        Activity context;
        List<Feed> list;
        public FeedAdapter(Activity _context, List<Feed> _list) : base()
        {
            this.context = _context;
            this.list = _list;
        }

        public override int Count
        {
            get { return list.Count; }
        }

        public override long GetItemId(int position)
        {
            return list[position].Feed_Id;
        }

        public override Feed this[int index]
        {
            get { return list[index]; }
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {

            View view = convertView;
            ViewHolder holder;
            Feed item = this[position];
            //if (view == null)
            //{
                view = context.LayoutInflater.Inflate(Resource.Layout.feedRows, null);

            //}

            holder = new ViewHolder();
            holder.Initialize(view);
            view.Tag = holder;

            holder = (ViewHolder)view.Tag;
            holder.Bind(context, item.Feed_Id, item.Photo, position);

            return view;
        }

        private class ViewHolder : Java.Lang.Object
        {
            TextView UserName;
            ImageView UserImg, Img, ImgValue, ImgLikes;
            ImageView ShowCommentBox;

            internal void Bind(Activity context, int Feed_Id, string Photo, int position)
            {
                UserName.Text = "Ronique";
                Glide.With(context).Load(Photo).Into(Img); //this.photo can also be used

                ImgValue.Tag = Feed_Id;
                ImgLikes.Tag = Feed_Id;
                UserImg.Tag = Feed_Id;
                ShowCommentBox.Tag = Feed_Id;


                ImgValue.Click -= ImgValue_Click;
                ImgValue.Click += ImgValue_Click;
                ImgLikes.Click -= ImgLikes_Click;
                ImgLikes.Click += ImgLikes_Click;

                if (!UserImg.HasOnClickListeners)
                {
                    UserImg.Click += (sender, args) =>
                    {
                        int pos = (int)UserImg.Tag;
                        Intent i = new Intent(context, typeof(ViewPost));
                        i.PutExtra("PostId", pos.ToString());
                        context.StartActivity(i);
                    };
                }

                if (!ShowCommentBox.HasOnClickListeners)
                {
                    ShowCommentBox.Click += (sender, args) =>
                    {
                        int pos = (int)ShowCommentBox.Tag;
                        Intent i = new Intent(context, typeof(CommentsActivity));
                        i.PutExtra("PostId", pos.ToString());
                        context.StartActivity(i);
                    };
                }

            }

            internal void Initialize(View view)
            {
                UserName = view.FindViewById<TextView>(Resource.Id.txtName);
                UserImg = view.FindViewById<ImageView>(Resource.Id.imgUser);
                Img = view.FindViewById<ImageView>(Resource.Id.imgPhoto);
                ImgValue = view.FindViewById<ImageView>(Resource.Id.btnValues);
                ImgLikes = view.FindViewById<ImageView>(Resource.Id.btnLikes);
                ShowCommentBox = view.FindViewById<ImageView>(Resource.Id.btnComment);

            }

            private void ImgLikes_Click(object sender, EventArgs e)
            {
                int pos = (int)ImgLikes.Tag;
                ImgLikes.SetImageResource(Resource.Drawable.ic_likes);
            }

            private void ImgValue_Click(object sender, EventArgs e)
            {
                int pos = (int)((ImageView)sender).Tag;
                ImgValue.SetImageResource(Resource.Drawable.ic_values_1);
            }

        }

    }
}

Now the one bellow scrolls smoothly but when i make click event for a imageview to SetImageResource it also click others that is out of views and when you scroll you can also see that that imageview was also clicked Set to the image source of the first click item.

Code

using System;
using System.Collections.Generic;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Com.Bumptech.Glide;

namespace FunStock
{
    class FeedAdapter : BaseAdapter<Feed>
    {
        Activity context;
        List<Feed> list;
        public FeedAdapter(Activity _context, List<Feed> _list) : base()
        {
            this.context = _context;
            this.list = _list;
        }

        public override int Count
        {
            get { return list.Count; }
        }

        public override long GetItemId(int position)
        {
            return list[position].Feed_Id;
        }

        public override Feed this[int index]
        {
            get { return list[index]; }
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {

            var view = convertView;
            FeedAdapterViewHolder holder = null;
            Feed item = this[position];

            if (view != null)
                holder = view.Tag as FeedAdapterViewHolder;
            if (holder == null)
            {
                holder = new FeedAdapterViewHolder();
                var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
                view = inflater.Inflate(Resource.Layout.feedRows, parent, false);

                holder.UserName = view.FindViewById<TextView>(Resource.Id.txtName);
                holder.UserImg = view.FindViewById<ImageView>(Resource.Id.imgUser);
                holder.Img = view.FindViewById<ImageView>(Resource.Id.imgPhoto);
                holder.ImgValue = view.FindViewById<ImageView>(Resource.Id.btnValues);
                holder.ImgLikes = view.FindViewById<ImageView>(Resource.Id.btnLikes);
                holder.ShowCommentBox = view.FindViewById<ImageView>(Resource.Id.btnComment);
                view.Tag = holder;

            }


            holder.UserName.Text = "Ronique";
            Glide.With(context).Load(list[position].Photo).Into(holder.Img); //this.photo can also be used

            holder.ImgValue.Tag = list[position].Feed_Id;
            holder.ImgLikes.Tag = list[position].Feed_Id;
            holder.UserImg.Tag = list[position].Feed_Id;
            holder.ShowCommentBox.Tag = list[position].Feed_Id;


            if (!holder.UserImg.HasOnClickListeners)
            {
                holder.UserImg.Click += (sender, args) =>
                {
                    int pos = (int)holder.UserImg.Tag;
                    Intent i = new Intent(context, typeof(ViewPost));
                    i.PutExtra("PostId", pos.ToString());
                    context.StartActivity(i);
                };
            }

            if (!holder.ShowCommentBox.HasOnClickListeners)
            {
                holder.ShowCommentBox.Click += (sender, args) =>
                {
                    int pos = (int)holder.ShowCommentBox.Tag;
                    Intent i = new Intent(context, typeof(CommentsActivity));
                    i.PutExtra("PostId", pos.ToString());
                    context.StartActivity(i);
                };
            }

            if (!holder.ImgLikes.HasOnClickListeners)
            {
                holder.ImgLikes.Click += (sender, args) =>
                {
                    holder.ImgLikes.SetImageResource(Resource.Drawable.ic_likes);
                };
            }

            if (!holder.ImgValue.HasOnClickListeners)
            {
                holder.ImgValue.Click += (sender, args) =>
                {
                    holder.ImgValue.SetImageResource(Resource.Drawable.ic_values_1);
                };
            }


            return view;
        }


        internal class FeedAdapterViewHolder : Java.Lang.Object
        {
            public TextView UserName { get; set; }
            public ImageView UserImg { get; set; }
            public ImageView Img { get; set; }
            public ImageView ImgValue { get; set; }
            public ImageView ImgLikes { get; set; }
            public ImageView ShowCommentBox { get; set; }
        }

    }


}

Am hoping someone can help me with this.

Sign In or Register to comment.