具有自定义MvxAdapter的MvxListView在MvxFragment中不起作用

时间:2016-03-18 14:18:10

标签: c# xamarin mvvmcross

我试图在片段中显示带有自定义MvxAdapter的MvxListView,但ListView从不显示。我调试了,我的适配器中的GetView方法从未调用过。有人能帮助我吗?

我正在使用MvvmCross 4.0和MvvmCross.Droid.FullFragging命名空间的片段。

我的MvxAdapter

public class OcorrenciasListAdapter : MvxAdapter
{
    private Context _context;

    public OcorrenciasListAdapter(Context context, IMvxAndroidBindingContext bindingContext) : base(context, bindingContext)
    {
        _context = context;
    }      

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var inflater = LayoutInflater.From(_context);
        convertView  = inflater.Inflate(Resource.Layout.OcorrenciaItem, parent, false);
        var ocorrencia = (Ocorrencia) GetRawItem(position);
        var txtViewSintomas = convertView.FindViewById<TextView>(Resource.Id.txtViewSintomas);

        StringBuilder textoSintomas = new StringBuilder(ocorrencia.sintomas[0].nome);            

        if (ocorrencia.sintomas.Count >= 3)
        {
            for (int i = 1; i < 3; i++)
            {
                textoSintomas.Append(", ");
                textoSintomas.Append(ocorrencia.sintomas[i].nome);
            }
        }
        else
        {
            for (int i = 1; i < ocorrencia.sintomas.Count; i++)
            {
                textoSintomas.Append(", ");
                textoSintomas.Append(ocorrencia.sintomas[i].nome);
            }
        }

        txtViewSintomas.Text = textoSintomas.ToString();

        var txtViewData = convertView.FindViewById<TextView>(Resource.Id.txtViewData);
        txtViewData.Text = ocorrencia.cadastro.ToString("dd/MM/yyyy");

        if (ocorrencia.confirmadoMedico)
        {
            var txtViewConfirmado = convertView.FindViewById<TextView>(Resource.Id.txtViewConfirmacao);
            txtViewConfirmado.Text = "Confirmado pelo médico";
        }

        return convertView;
    }
}

我的片段:

public class OcorrenciaView : MvxFragment<OcorrenciaViewModel>
{
    private MvxListView _lstViewOcorrencias;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreateView(inflater, container, savedInstanceState);
        var view = this.BindingInflate(Resource.Layout.OcorrenciaView, null);
        _lstViewOcorrencias = view.FindViewById<MvxListView>(Resource.Id.lstViewOcorrencias);

        var btnAdicionar = view.FindViewById<FloatingActionButton>(Resource.Id.btnAdicionar);

        btnAdicionar.AttachToListView(_lstViewOcorrencias);

        _lstViewOcorrencias.Adapter = new OcorrenciasListAdapter(Activity, (IMvxAndroidBindingContext)this.BindingContext);

        return view;
    }
}

我的片段布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:local="http://schemas.android.com/apk/res-auto/AppDemia.Droid"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:gridSpec="1|8|#0093eeff|K:#ee8700ff:16,l:72,l:16,r|S:#83ee00ff:16,0,l:16,56,l:16,0,r"
android:background="@color/appdemia_branco"
android:orientation="vertical"
android:id="@+id/Content">
<com.refractored.fab.FloatingActionButton
    android:id="@+id/btnAdicionar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:src="@drawable/ic_add_mini"
    android:tint="@color/appdemia_branco"
    local:MvxBind="Click CriarOcorrenciaCommand"
    fab:fab_colorNormal="@color/appdemia_laranja"
    fab:fab_colorPressed="@color/appdemia_laranjaEscuro" />
<Mvx.MvxListView
    android:id="@+id/lstViewOcorrencias"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#aeaeae"
    android:dividerHeight="1px"
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:choiceMode="singleChoice"
    local:MvxBind="ItemsSource Ocorrencias; SelectedItem ObjEntidade; ItemClick ItenClickCommand; "
    local:MvxItemTemplate="@layout/ocorrenciaitem" />

我的列表视图布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/appdemia_branco"
android:orientation="vertical"
android:padding="5dp">
<TextView
    android:id="@+id/txtViewSintomas"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#606060"
    android:layout_alignParentLeft="true"
    android:typeface="sans"
    android:textSize="15dip"
    android:textStyle="bold" />
<TextView
    android:id="@+id/txtViewData"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtViewSintomas"
    android:layout_alignParentRight="true"
    android:textColor="#606060"
    android:typeface="sans"
    android:textSize="15dip"
    android:textStyle="bold" />
<TextView
    android:id="@+id/txtViewConfirmacao"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtViewSintomas"
    android:textColor="#343434"
    android:textSize="10dip"
    android:layout_marginTop="1dip"
    android:layout_alignParentLeft="true" />

1 个答案:

答案 0 :(得分:0)

最后我发现了问题:我的MvxFragment类(OcorrenciaView)中的ViewModel属性并未实现。我经常使用MvxActivity并且不需要实现ViewModel属性,因为基类(MvxActivity)实现了这一点,然后我认为MvxFragment具有相同的行为。现在,我只是在OcorrenciaView构造中实现ViewModel属性,并且MvxListView显示项目。

PS:我很抱歉英语错误。我是巴西人,我的英语非常有限。