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

Custom ListAdapter

$
0
0

Hi,

New to Xamarin and have a question on building a custom list adapter.

I have an ImageButton in my ListAdapter that instantiates a .Click event to show or hide more information.
Issue is that when I have more records than can be displayed on the phone and scroll it sometimes registers the .click event more than once.
It also registers the .click event more than once if I leave that layout page and go back to it.
This causes the more Information to be expanded/collapsed with a single click.

Any help would be appreciated.

Erik

Here is my ListAdapter Code Snippet;

    public ListLayoutAdapter (Activity context, List<ListLayout>items)
    {
        usedPos = new List<int>();
        this.context = context;
        this.items = items;
    }

    public override View GetView(int position, View convertView, ViewGroup parent) 
    {
        var item = items [position];
        View view = convertView;
        if (view == null) 
        {
            view = context.LayoutInflater.Inflate (Resource.Layout.ListViewTemplate, null);
        }
        // Get controls
        ImageView btImgView1 = view.FindViewById<ImageView> (Resource.Id.imgView1);
        TextView txtListMeter = view.FindViewById<TextView> (Resource.Id.txtListMeter);
        TextView txtAction = view.FindViewById<TextView> (Resource.Id.txtAction);
        TextView txtDate = view.FindViewById<TextView> (Resource.Id.txtDate);
        TextView txtMoreInfo = view.FindViewById<TextView> (Resource.Id.txtMoreInfo);
        ImageButton btMoreInfo = view.FindViewById<ImageButton> (Resource.Id.btMoreInfo);
        // Set the columns in the Listview
        btImgView1.SetImageResource (item._listIcon);
        txtListMeter.Text = item._listMeter;
        txtAction.Text = item._listAction;
        //
        // Set color of the action
        //
        if (item._listAction == RDM.Globals.MeterActions.Connect.ToString()) {
            txtAction.SetTextColor (Android.Graphics.Color.Green);
        }
        else if (item._listAction == RDM.Globals.MeterActions.Disconnect.ToString())
        {
            txtAction.SetTextColor (Android.Graphics.Color.Red);
        }
        else
        {
            txtAction.SetTextColor (Android.Graphics.Color.Rgb (49, 93, 116));
        }
        txtDate.Text = item._curDate;
        txtMoreInfo.Text = item._moreInfo;
        //
        btMoreInfo.Focusable = false;
        btMoreInfo.FocusableInTouchMode = false;
        btMoreInfo.Clickable = true;
        //
        // Make all the columns of the listview rows clickable to expand/collapse
        // 
        if (item._listAction == string.Empty )
            btMoreInfo.Visibility = ViewStates.Invisible;
        if (!usedPos.Contains (position) && item._listAction !=string.Empty) 
        {
            Console.WriteLine ("ListLayout Postion: " + position.ToString ());
            usedPos.Add (position);
            btMoreInfo.Click += (object sender, EventArgs e) => {
                view.FindViewById<TextView> (Resource.Id.txtMoreInfo).Visibility = 
                (view.FindViewById<TextView> (Resource.Id.txtMoreInfo).Visibility == ViewStates.Visible ? ViewStates.Gone : ViewStates.Visible);
                if(view.FindViewById<TextView> (Resource.Id.txtMoreInfo).Visibility == ViewStates.Visible)
                {
                  view.FindViewById<ImageView>(Resource.Id.btMoreInfo).SetBackgroundResource(Resource.Drawable.up);
                }
                else
                {
                    view.FindViewById<ImageView>(Resource.Id.btMoreInfo).SetBackgroundResource(Resource.Drawable.down);
                }
            };
        }

        return view;
    }

Page that has ListView Template snippet:

...
...
...
...
...
... <TextView...
... android:text="Medium Text"
... android:textAppearance="?android:attr/textAppearanceMedium"
... android:layout_width="255dp"
... android:layout_height="30dp"
... android:layout_below="@id/txtListMeter"
... android:layout_toRightOf="@id/imgView1"
... android:gravity="left"
... android:id="@+id/txtAction" />
...
...
...
...

Page that displays the Listview

...
...
...
...
...
...
...
...
...


Viewing all articles
Browse latest Browse all 58056

Trending Articles



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