You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MvxListView的MvxAdapter GetView()未调用,求交替图片绑定方案

解决MvxListView自定义适配器GetView()未调用的问题

嘿,刚上手Xamarin Android和MvvmCross遇到这种问题很正常,我帮你梳理几个最容易遗漏的配置点,一步步排查解决:

1. 确保自定义适配器的继承与构造函数正确

自定义适配器必须继承MvxAdapter,并且构造函数要传入Context和IMvxAndroidBindingContext——这是MvvmCross适配器的核心依赖,少了它绑定逻辑会失效,GetView自然不会触发。示例代码如下:

using Android.Content;
using Android.Views;
using Android.Widget;
using MvvmCross.Binding.Droid.BindingContext;

namespace YourAppNamespace.Droid.Adapters
{
    public class AlternateImageAdapter : MvxAdapter
    {
        public AlternateImageAdapter(Context context, IMvxAndroidBindingContext bindingContext) 
            : base(context, bindingContext)
        {
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            // 先调用base.GetView获取绑定好的item视图
            var itemView = base.GetView(position, convertView, parent);
            
            // 在这里实现交替图片逻辑
            var imageView = itemView.FindViewById<ImageView>(Resource.Id.iv_list_item_image);
            if (position % 2 == 0)
            {
                imageView.SetImageResource(Resource.Drawable.img_odd_row);
            }
            else
            {
                imageView.SetImageResource(Resource.Drawable.img_even_row);
            }
            
            return itemView;
        }
    }
}

2. 正确将自定义适配器绑定到MvxListView

有两种方式可以给MvxListView设置自定义适配器:

方式一:在布局文件中指定

确保布局里的MvxListView同时设置了ItemsSource绑定、MvxItemTemplate(item模板)和自定义适配器的完整命名空间:

<Mvx.MvxListView
    android:id="@+id/lv_items"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"
    local:MvxBind="ItemsSource YourItemsCollection"
    local:MvxItemTemplate="@layout/list_item_template"
    local:MvxAdapter="YourAppNamespace.Droid.Adapters.AlternateImageAdapter" />

方式二:在代码中手动实例化

在Activity/Fragment的OnCreate(或OnViewCreated)方法中,找到ListView并直接设置适配器:

var listView = FindViewById<MvxListView>(Resource.Id.lv_items);
listView.Adapter = new AlternateImageAdapter(this, this.BindingContext);

3. 检查ViewModel的ItemsSource是否有效

这是最容易被忽略的点:如果ViewModel中绑定的集合是空的,或者没有正确实现通知属性,MvxListView不会触发GetView。确保你的ViewModel里用MvxObservableCollection(支持自动通知UI更新)来存储数据:

using MvvmCross.ViewModels;
using MvvmCross.Collections;

namespace YourAppNamespace.ViewModels
{
    public class YourViewModel : MvxViewModel
    {
        private MvxObservableCollection<YourItemModel> _yourItemsCollection;
        public MvxObservableCollection<YourItemModel> YourItemsCollection
        {
            get => _yourItemsCollection;
            set => SetProperty(ref _yourItemsCollection, value);
        }

        public override void Prepare()
        {
            // 初始化数据,确保集合不为空
            YourItemsCollection = new MvxObservableCollection<YourItemModel>
            {
                new YourItemModel(),
                new YourItemModel(),
                // 添加更多测试数据
            };
        }
    }
}

4. 确认item模板布局的正确性

你的item模板(比如list_item_template.axml)必须包含你要操作的ImageView,并且ID要和适配器中FindViewById的ID一致:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_list_item_image"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:scaleType="CenterCrop"/>

    <!-- 其他控件,比如文本等 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        local:MvxBind="Text ItemTitle"/>

</LinearLayout>

按照上面的步骤排查,基本就能解决GetView不调用的问题了。如果还是不行,可以检查一下MvvmCross的版本是否适配,或者有没有在Setup.cs中错误地覆盖了适配器的注册逻辑。

内容的提问来源于stack exchange,提问作者Nadeem Shaikh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:08:40