我是Xamarin Android的新手,对MvvmCrosss也是如此。我想将备用图像绑定到我的MvxListView行,并且我试图使用自定义适配器。但无论如何我知道我错过了一些东西,所以试图找到它的方法。
我的MvxListview代码如下 -
<Mvx.MvxListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@null"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_above="@+id/llSubtotal"
android:cacheColorHint="@android:color/transparent"
local:MvxBind="ItemsSource Items;ItemClick ItemSelectedCommand"
local:MvxItemTemplate="@layout/listviewitem"
android:id="@+id/mvxLVCustomerItemList" />
在上面的代码中,我为listview定义了ItemsSource和Itemtemplate。 对于ItemsSoure,我使用了viewmodel,如下所示 -
public class HomeViewModel : MvxViewModel
{
private readonly IRetailService _retailService;
private IList<RetailItem> _items;
public HomeViewModel(IRetailService RetailService)
{
_retailService = RetailService;
}
public override void Start()
{
IsLoading = true;
_retailService.GetFeedItems(OnGarfieldItems, OnError);
}
private void OnGarfieldItems(IList<RetailItem> list)
{
IsLoading = false;
Items = list;
}
public IList<RetailItem> Items
{
get { return _items; }
set { _items = value; RaisePropertyChanged(() => Items); }
}
public IMvxCommand ItemSelectedCommand => new MvxCommand<string>(DoSelectItem);
private void DoSelectItem(string item)
{
}
}
我的模板代码如下 -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:id="@+id/lvItemTemplate"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/odd">
<Mvx.MvxImageView
android:layout_width="150dp"
android:layout_height="fill_parent"
android:id="@+id/ivItemImage"
android:textSize="40dp"
android:layout_gravity="center_horizontal|center"
local:MvxBind="ImageUrl StripUrl" />
<TextView
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:id="@+id/tvItemName"
android:layout_height="fill_parent"
local:MvxBind="Text Title" />
</LinearLayout>
以下是我正在使用的适配器 -
public class CustomAdapter : MvxAdapter
{
public CustomAdapter(Context context, IMvxAndroidBindingContext bindingContext) : base(context, bindingContext)
{
}
protected override View GetView(int position, View convertView, ViewGroup parent, int templateId)
{
var v = base.GetView(position, convertView, parent);
LayoutInflater inflaterRMH = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
LinearLayout llTemplate = (LinearLayout)v.FindViewById(Resource.Id.lvItemTemplate);
llTemplate.SetBackgroundResource(position % 2 == 0 ? Resource.Drawable.odd : Resource.Drawable.even);
return v;
}
}
如上面的代码所示,我正在尝试更改listview中应用的模板的linearlayout的背景图像。
活动如下 -
public class HomeView : MvxAppCompatActivity, IOnClickListener
{
private ListView lvRMH;
public HomeViewModel HomeViewModel
{
get { return (HomeViewModel)base.ViewModel; }
}
protected override void OnViewModelSet()
{
SetContentView(Resource.Layout.Home);
lvRMH = (ListView)FindViewById(Resource.Id.mvxLVRMHList);
lvRMH.Adapter = new CustomAdapter(this, (IMvxAndroidBindingContext)BindingContext);
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
}
}
我知道我在理解MvvmCross和Android时遇到了一些可怕的错误,我认为在将Itemsource绑定到Listview之前调用了GetView()。但如果是这样的话,它的解决方法是什么。如果可以在MvxListview中实现奇偶行而不通过ViewModel更改Listview的itemsource绑定,则可以使用我的代码。
任何帮助都将受到高度赞赏。
由于
答案 0 :(得分:0)
最后,我现在可以调用GetView。我不完全确定,但我认为这个问题与线程有关,因为我在输出窗口中看到了以下消息 -
跳过300帧!应用程序可能在其上做了太多工作 主线。
我刚刚搜索过它。然后我尝试从OnCreate方法一步一步地进行调试,之后我的调试器继续使用GetView方法。是的,它是线程相关的问题,因为没有调用GetView方法。实际上我的列表数据是使用HttpClient加载的,并且需要时间来获得响应。所以这只是线程相关的问题。
感谢。