Ad

Problem Changing The UI Of A Specific Cell Inside A Gridview That Is Inside A View Pager

So, I have this problem with the UI, I made a calendar, wich basicaly consists on a Gridview inside a ViewPager, and I want to show to highlight/make standout, the cell that show the current day. I managed to do that, the problem comes when the user swipes the ViewPager, the highlighted cell remains highlighted even though the date isn't correct (look at the pictures to understand). I have 4 fragments that are reused to make the calendar, so when I swipe 4 times and then swipe back the problem disapears, so I think the problem is in the way I'm refreshing the ui on the adapter, but I don't know how to solve this.

On startup

On swipe left(has you can see the day 12 is highligted)

Here's the code of my adapter:

    public void UpdateToday() { today = DateTime.Today; }

    public override DateTime this[int position] 
    {
        get { return datetimeList[position]; }
    }

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

    public override long GetItemId(int position) 
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent) 
    {
        View module = convertView;

        if (module == null)
        {
            module = LayoutInflater.From(context).Inflate(Resource.Layout.calendar_gridcell, null, false);
        }
        var textView = module.FindViewById<TextView>(Resource.Id.calendar_cell);


        if (datetimeList[position].Month == month)
            textView.SetTextColor(new Android.Graphics.Color(context.GetColor(Resource.Color.colorSecondary)));
        else            
            textView.SetTextColor(new Android.Graphics.Color(context.GetColor(Resource.Color.colorTextDarkSemiTransparent)));

        if (datetimeList[position].Month == today.Month && datetimeList[position].Day == today.Day)
        {
            textView.Background = context.GetDrawable(Resource.Drawable.cell_today);
            textView.SetTextColor(new Android.Graphics.Color(context.GetColor(Resource.Color.colorAccent)));
        }             

        textView.Text = datetimeList[position].Day.ToString();
        module.SetPadding(8, 8, 8, 8);

        return module;
    }

Edit

So the sulution was really simple, I just needed an else statement in the If that compared the item date to today's date:

        if (dateTimeList[position].Date == DateTime.Today)
        {
            textView.Background = context.GetDrawable(Resource.Drawable.cell_today);
            textView.SetTextColor(new Android.Graphics.Color(context.GetColor(Resource.Color.colorAccent)));
        }
        else
        {
            if (dateTimeList[position].Month == month)
                textView.SetTextColor(new Android.Graphics.Color(context.GetColor(Resource.Color.colorSecondary)));
            else
                textView.SetTextColor(new Android.Graphics.Color(context.GetColor(Resource.Color.colorTextDarkSemiTransparent)));

            textView.Background = context.GetDrawable(Resource.Drawable.cell_background);
        }
Ad

Answer

The reason I found was very simple what is happening here is that on the basis of DateTime.Today you first mark today's date.

But when you know you slide from one page to the other you are not un-marking that date so what happens is in this case the current date is marked but when you move to the next screen you are not un-marking the background so it still stays the same.

Revert in case this does not make sense.

Ad
source: stackoverflow.com
Ad