在 MvvmCross 中,是否可以有一个顶部带有搜索 EditText 的 Android MvxListView?如何?
在您的 View.axml 中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
local:MvxBind="Text SearchString" />
<Mvx.MvxListView
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="ItemsSource FilteredList" />
</LinearLayout>
非常简单,EditText 是您的搜索查询框,其下方的列表本身将是您的过滤列表。
在您的 ViewModel.cs 中:
public class FirstViewModel
: MvxViewModel
{
public FirstViewModel()
{
_filteredList = _completeList;
}
private string _searchString;
public string SearchString
{
get { return _searchString; }
set
{
_searchString = value;
if (string.IsNullOrEmpty(value))
{
_filteredList = _completeList;
}
else
{
_filteredList = _completeList.Where(o => o == value).ToList();
}
RaisePropertyChanged(() => SearchString);
RaisePropertyChanged(() => FilteredList);
}
}
private List<string> _completeList = new List<string>() { "a", "b", "c", "d", "e" };
private List<string> _filteredList;
public List<string> FilteredList
{
get { return _filteredList; }
}
}
这里的 ViewModel 从 EditText 接收 SearchString,然后使用 Linq 过滤完整列表。然后,它获取已过滤的列表和已过滤列表的 RaisesPropertyChanged。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)