GetView updating items outside of screen

81 Views Asked by At

I have following code inside GetView method of my Custom adapter:

public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var view = convertView ?? activity.LayoutInflater.Inflate(
                       Resource.Layout.ScannedTuListItem, parent, false);

        var scannedTu = view.FindViewById<TextView>(Resource.Id.scannedTu);

        var tuTxt = activity.Resources.GetString(Resource.String.tu);
        var custTxt = activity.Resources.GetString(Resource.String.customer);
        var orderTxt = activity.Resources.GetString(Resource.String.order);
        var targetBinTxt = activity.Resources.GetString(Resource.String.targetBin);

        scannedTu.Text = 
                        $"{tuTxt} {tus[position].No}" +
                        $"\n{custTxt} {tus[position].Customer} / {orderTxt} {tus[position].Order}" +
                        (string.IsNullOrEmpty(tus[position].TargetBin) ?
                        string.Empty : $"\n{targetBinTxt} {tus[position].TargetBin}");

        if (tus[position].AtPackingStation && !tus[position].Ready)
        {
            scannedTu.SetBackgroundColor(Color.Yellow);
            scannedTu.SetTextColor(Color.Black);
        }
        else if (tus[position].AtPackingStation && tus[position].Ready || tus[position].ScanOk == true)
        {
            scannedTu.SetBackgroundColor(Color.Green);
            scannedTu.SetTextColor(Color.Black);
        }
        else if (tus[position].ScanOk == false)
        {
            scannedTu.SetBackgroundColor(Color.Red);
            scannedTu.SetTextColor(Color.Black);
        }

        return view;
    }

When I change Property ScanOK and Ready to True, say, for the first 3 items in the list, it automatically sets the background color of items outside the screen also to Green.

How do I make it color only the first 3 items without affecting items off the screen?

1

There are 1 best solutions below

2
FreakyAli On

When I change Property ScanOK and Ready to True, say, for the first 3 items in the list, it automatically sets the background color of items outside the screen also to Green.

How do I make it color only the first 3 items without affecting items off the screen?

For that you will have to add an initial condition on the basis of position for eg

   If(position>2) //since integers start from zero
   return;

Also if you want to mark on the basis of how many list items are displayed on the screen you can check this SO answer