更改视图寻呼机内 gridview 内特定单元格的 UI 时出现问题
Problem changing the UI of a specific cell inside a gridview that is inside a view pager
所以,我在 UI 中遇到了这个问题,我制作了一个日历,它基本上包含在 ViewPager 内的 Gridview 上,我想向 highlight/make standout 展示那个单元格显示当前日期。我设法做到了,当用户滑动 ViewPager 时,问题就来了,即使日期不正确,突出显示的单元格仍然突出显示(查看图片以了解)。
我有 4 个片段可重复用于制作日历,所以当我滑动 4 次然后向后滑动时,问题消失了,所以我认为问题出在我刷新适配器上的 ui 的方式上,但是我不知道如何解决这个问题。
On startup
On swipe left(你能看到第12天被高亮了吗)
这是我的适配器代码:
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;
}
编辑
所以解决方案非常简单,我只需要在 If 中添加一个 else 语句,将项目日期与今天的日期进行比较:
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);
}
我发现的原因很简单这里发生的事情是在 DateTime.Today 的基础上你首先标记今天的日期。
但是当你知道你从一页滑到另一页时,你并没有取消标记那个日期,所以在 this case the current date is marked but when you move to the next screen 中发生的事情是你没有取消标记背景,所以它仍然保持不变。
如果这没有意义,请还原。
所以,我在 UI 中遇到了这个问题,我制作了一个日历,它基本上包含在 ViewPager 内的 Gridview 上,我想向 highlight/make standout 展示那个单元格显示当前日期。我设法做到了,当用户滑动 ViewPager 时,问题就来了,即使日期不正确,突出显示的单元格仍然突出显示(查看图片以了解)。 我有 4 个片段可重复用于制作日历,所以当我滑动 4 次然后向后滑动时,问题消失了,所以我认为问题出在我刷新适配器上的 ui 的方式上,但是我不知道如何解决这个问题。
On startup
On swipe left(你能看到第12天被高亮了吗)
这是我的适配器代码:
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;
}
编辑
所以解决方案非常简单,我只需要在 If 中添加一个 else 语句,将项目日期与今天的日期进行比较:
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);
}
我发现的原因很简单这里发生的事情是在 DateTime.Today 的基础上你首先标记今天的日期。
但是当你知道你从一页滑到另一页时,你并没有取消标记那个日期,所以在 this case the current date is marked but when you move to the next screen 中发生的事情是你没有取消标记背景,所以它仍然保持不变。
如果这没有意义,请还原。