2016-04-29 69 views
0

我有一个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> 

请记住,我没有写出扩展视图的代码。我不想从那开始,直到我真正有了我可以使用的观点。

谢谢。

回答

1

您需要在ContactsListView中实施AdapterView.IOnItemClickListener,以便您可以接收被单击的项目。

但是这需要一些定制,因为实施AdapterView.IOnItemClickListener似乎抵消MvvmCross ICommand的结合。

综观MvxListView我们可以看到用于调用项目ICommand代码的code:在我们OnItemClick

public void OnItemClick(AdapterView parent, View view, int position, long id) 
{ 
    MvxListView listView = parent as MvxListView; 
    if (listView == null) 
     return; 

    var item = listView.Adapter.GetRawItem(position); 

    if(item == null) 
     return; 

    if (!listView.ItemClick.CanExecute(item)) 
     return; 

    listView.ItemClick.Execute(item); 

    // Write your code here for expanding the view 
} 

protected virtual void ExecuteCommandOnItem(ICommand command, int position) 
{ 
    if (command == null) 
     return; 

    var item = this.Adapter.GetRawItem(position); 
    if (item == null) 
     return; 

    if (!command.CanExecute(item)) 
     return; 

    command.Execute(item); 
} 

实现这个我不知道,如果是这样的要走的路,但它是实现你想要的东西的一种方法。

0

ItemClickCommand只会给你ViewModel而不是视图。为了做到这一点,您需要创建自己的绑定,以返回ItemClick上的视图,类似于MvvmCross附带的视图。或者订阅ListView上的ItemClicked事件并处理那里的东西。

前者可能不是最好的主意,因为它不会是平台不可知的,你必须从视图设置命令,这对我来说似乎有点愚蠢。

因此,我猜想MvxListView/ListView上的ItemClick事件的简单订阅将能够完成返回视图的工作。

另一种方法是通过视图单元格的绑定ViewModel的属性切换展开,该视图单元格可以通过动画或任何您喜欢的方式切换可见性。

0

我试过@Pilatus解决方案,它似乎有一些额外的工作。在实现AdapterView.IOnItemClickListener后,我最终使用了OnItemClick方法。我必须做的额外事情是设置_listView。OnItemClickListener = this; (这是实现AdapterView.IOnItemClickListener的类)。然后,我可以使用传入该方法的视图并对其进行处理。

另外,作为一个附加说明,ICommand绑定没有被取消。在我的实现中,两个事件都被触发了!

相关问题