我有一個MvxListView,正確工作並顯示我需要的信息。我甚至有我的ItemClick命令正常工作。我現在弄不清楚的是如何獲取用戶點擊的視圖單元格。我想展開用戶點擊的視圖單元格。下面是一些代碼示例什麼我的工作:如何返回使用MvxListView單擊的ListView項目?
視圖模型:
private List<ContactItemEncrypted> _contactsEncryted;
public List<ContactItemEncrypted> ContactsEncrypted
{
get { return _contactsEncryted; }
set { _contactsEncryted = value; RaisePropertyChanged(() => ContactsEncrypted); }
}
private MvxCommand<ContactItemDecrypted> _itemSelectedCommand;
public System.Windows.Input.ICommand ItemSelectedCommand
{
get
{
_itemSelectedCommand = _itemSelectedCommand ?? new MvxCommand<ContactItemDecrypted>(DoSelectItem);
return _itemSelectedCommand;
}
}
private void DoSelectItem(ContactItemDecrypted item)
{
var message = new ContactSelectedMessage(this, item);
_messenger.Publish(message);
}
查看:
[Activity(
Label = "Contacts",
Theme = "@style/AppTheme",
ScreenOrientation = ScreenOrientation.Portrait
)]
public class ContactsListView : MvxAppCompatActivity
{
private MvxListView _listView;
private MvxSubscriptionToken _tokenContactSelected;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.ContactsListScreen);
_listView = FindViewById<MvxListView>(Resource.Id.contactListView);
var messenger = Mvx.Resolve<IMvxMessenger>();
_tokenContactSelected = messenger.SubscribeOnMainThread<ContactSelectedMessage>(ContactSelected);
}
private void ContactSelected(ContactSelectedMessage obj)
{
ExpandView(_listView.Adapter.GetPosition(obj.SelectedContact));
}
private void ExpandView(int index)
{
itemLayout.Visibility = ViewStates.Visible;
}
}
使用ListView的佈局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<Mvx.MvxListView
android:id="@+id/contactListView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="ItemsSource ContactsDecrypted; ItemClick ItemSelectedCommand; ItemLongClick ItemLongClickCommand"
local:MvxItemTemplate="@layout/contactlistviewitemtemplate" />
</FrameLayout>
而ItemTemplate佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="@color/black"
android:layout_margin="14dp"
custom:MvxBind="Text FullName" />
</LinearLayout>
請記住,我沒有寫出擴展視圖的代碼。我不想從那開始,直到我真正有了我可以使用的觀點。
謝謝。