在MvvmCross中,可以在顶部使用带有Search EditText的Android MvxListView吗?怎么样?
答案 0 :(得分:6)
在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用于过滤后的List。
答案 1 :(得分:4)
为sjk +1。您可以使用SearchView并按代码设置SearchString属性,我正在使用它:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:visibility="visible"
android:background="@android:color/darker_gray" />
</LinearLayout>
<Mvx.MvxListView
android:id="@+id/ItemsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="ItemsSource FilteredList"/>
代码:
public class MyListFragment : MvxFragment, SearchView.IOnQueryTextListener
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
.........
var searchView = FindViewById<SearchView>(Resources.Id.search_view);
searchView.SetOnQueryTextListener(this);
searchView.SetIconifiedByDefault(false);
searchView.Focusable = false;
return view;
}
public bool OnQueryTextSubmit(String query)
{
return false;
}
public bool OnQueryTextChange(String newText)
{
(MyViewModelType)ViewModel.SearchString = newText;
return true;
}
}
我正在使用片段,但您可以在活动中使用它。