【发布时间】:2014-11-06 21:23:48
【问题描述】:
为了帮助进行讨论,我在 GitHub 上发布了所有相关的源代码... https://github.com/WindSpirit/DroidCustomView
基本上,我编写了一个 Android Custom-View (Asgl.Android.Views.RatingView),它继承自 TableLayout 并包含几个由公共 get/set 属性更改的 ImageView(通过 MvvmCross 绑定和/或 AXML 属性)。
这个Custom-View在一个MvxList、MvxItemTemplate中使用。
这是 MvxItemTemplate AXML 代码的样子...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/task_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="Text RatingValue" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="Text ScaleValue" />
<Asgl.Android.Views.RatingView
android:id="@+id/rating"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Rating RatingValue; Scale ScaleValue" />
</LinearLayout>
TextView 控件按预期通过 MvvmCross 绑定显示数据,但 Custom-View 控件显示不正确。
基于“N-28-CustomBinding”,当在 View 对象上调用 Public Property setter 方法时,我自定义了 Custom-View 的外观。另见http://slodge.blogspot.ca/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html
一切正常,除了所有初始 MvxItemTemplate 项目的显示完全相同,即使它们应该以不同的方式显示。就好像 Custom-View 的缓存图像用于每个列表项,即使列表项属性的值不同。
在 Custom-View 中,this.Parent() 永远不可用,并调用 Custom-View 方法,如 this.Invalidate() 或 this.InvalidateDrawable(this.Invalidate() 或 this.InvalidateDrawable()。 myDrawable.Drawable ),似乎没有任何效果。
也就是说,简单的执行下面的代码,在MvxList的构建过程中不一定会导致视觉列表项的变化……
public int? Rating
{
get { return _rating; }
set {
if ((value != null) && (value < -2 || value > 5))
throw new ArgumentOutOfRangeException();
if (_rating != value)
{
_rating = value;
_isUpdating = true;
try
{
var testValue = Scale ?? 0;
Rating0.Visibility = (testValue > 0) ?
ViewStates.Visible : ViewStates.Gone;
Rating1.Visibility = (testValue > 0) ?
ViewStates.Visible : ViewStates.Gone;
Rating2.Visibility = (testValue > 2) ?
ViewStates.Visible : ViewStates.Gone;
Rating3.Visibility = (testValue > 3) ?
ViewStates.Visible : ViewStates.Gone;
Rating4.Visibility = (testValue > 4) ?
ViewStates.Visible : ViewStates.Gone;
ActnGood.SetColorFilter((Rating > 0) ?
ColourGood : ColourDisabled, PorterDuff.Mode.SrcIn);
Rating0.SetImageDrawable(ActnGood);
ActnBad.SetColorFilter((Rating > 0) ?
ColourDisabled : ColourBad, PorterDuff.Mode.SrcIn);
Rating1.SetImageDrawable(ActnBad);
this.Invalidate();
// etc...
if (RatingChanged != null)
RatingChanged(this, EventArgs.Empty);
} finally {
_isUpdating = false;
}
}
}
}
public event EventHandler RatingChanged;
问题:当列表项属性值由于 MvvmCross 绑定而发生更改时,我需要做什么才能让 MvxList 重绘其中一个列表项?
【问题讨论】:
标签: android xamarin xamarin.android mvvmcross