我希望在我正在编写的 Xamarin.Android 应用程序中实现 MvxExpandableListView 的使用。我也在使用 MvvmCross。我可以让 ListView 显示标题列表,但当用户尝试展开标题时,应用程序崩溃了。
View的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/backgroundAssignmentTasks"
android:alpha="0.7"
android:src="@drawable/background" />
<Mvx.MvxExpandableListView
android:id="@+id/listAssignmentTasks"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_margin="16dp"
android:dividerHeight="7dp"
android:divider="@drawable/list_divider"
local:GroupItemTemplate="@layout/list_assignment_header"
local:MvxItemTemplate="@layout/list_assignment_detail"
local:MvxBind="ItemsSource Tasks; ItemClick AssignmentSelectedCommand" />
</RelativeLayout>
表头布局文件
<?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:id="@+id/layoutTaskName"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/specific_lightgrey">
<TextView
android:id="@+id/assignmentTaskName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="18dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
local:MvxBind="Text Title" />
</LinearLayout>
细节布局
<?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:id="@+id/layoutTaskDescription"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/specific_darkgrey"
android:visibility="visible">
<TextView
android:id="@+id/assignmentTaskDescription"
android:layout_width="match_parent"
android:layout_height="70dp"
android:textColor="@android:color/black"
android:textSize="18dp"
android:layout_margin="8dp"
android:lines="4"
local:MvxBind="Text Description" />
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_gravity="center"
android:id="@+id/buttonRecordVideo"
android:textColor="@android:color/white"
android:textSize="18dp"
android:text="Record"
android:background="@color/specific_red" />
</LinearLayout>
我的 ViewModel 提供了以下列表
private List<Assignment> _tasks;
public List<Assignment> Tasks
{
get { return _tasks; }
set {
_tasks = value;
RaisePropertyChanged ("Tasks");
}
}
抛出的异常是
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.ArgumentNullException: Argument cannot be null.
[MonoDroid] Parameter name: source
[MonoDroid] at System.Linq.Check.Source (object) <IL 0x00010, 0x0005f>
[MonoDroid] at System.Linq.Enumerable.Cast<object> (System.Collections.IEnumerable) <0x0002b>
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.Views.MvxExpandableListAdapter.GetChildrenCount (int) <IL 0x0000c, 0x000e7>
[MonoDroid] at Android.Widget.IExpandableListAdapterInvoker.n_GetChildrenCount_I (intptr,intptr,int) [0x00009] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.IExpandableListAdapter.cs:295
[MonoDroid] at (wrapper dynamic-method) object.4fad6314-d1a6-41b1-be64-97483d3976ab (intptr,intptr,int) <IL 0x00017, 0x0002f>
这让我相信我需要编写 MvxExpandableListAdapter 的实现。
如何将其链接到我的 ViewModel 和/或布局文件?
最佳答案
它看起来像你的“任务”属性,当你膨胀布局时它是空的。 确保在构造函数中将值分配给 _tasks。
public ViewModelCtor() {
_tasks = new List<Assignment>();
}
关于android - 使用 MvxExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31455861/