2016-11-17 122 views
1

我目前遇到与Android上的MvvmCross奇怪的问题。我想要将视图绑定到ViewModel。我的设置如下MvvmCross Databinding没有按预期工作

public class ForumTopicItemsViewModel : MvxViewModel 
{ 
    ObservableCollection<ForumReplyListModel> _forumComments; 
    ForumTopicDetailsModel _topic; 
    long _forumId; 
    long _topicId; 
    bool _busy; 
    int _page; 

    public ForumTopicItemsViewModel() 
    { 
     _topic = new ForumTopicDetailsModel() 
     { 
      DateCreated = DateTime.Now.AddDays(-1), 
      Discussion = "Just the fact that I dont care about what is displayed here", 
      ForumId = ForumId, 
      TopicId = TopicId, 
      Topic = "Another Heading, Just for the sake of it" 
     }; 
     _forumComments = new ObservableCollection<ForumReplyListModel> 
     { 
      new ForumReplyListModel() 
      { 
       CreatedBy = "Peter Edike", 
       DateCreated = DateTime.Now.AddDays(-1), 
       Reply = "Just a reply to show how far I have come" 
      } 
     }; 
    } 

    public int Page 
    { 
     get { return _page; } 
     set { SetProperty(ref _page, value); } 
    } 

    private static bool IsHostReachable() 
    { 
     var nReachability = Mvx.Resolve<IMvxReachability>(); 
     return nReachability.IsHostReachable(AppConfiguration.RequestUrl); 
    } 

    public ObservableCollection<ForumReplyListModel> Comments 
    { 
     get { return _forumComments; } 
     set { SetProperty(ref _forumComments, value); } 
    } 

    public ForumTopicDetailsModel Topic 
    { 
     get { return _topic; } 
     set { SetProperty(ref _topic, value); } 
    } 

    public string Heading => this.Topic.Topic; 
    public string Description => this.Topic.Discussion; 
    public string DateCreatedString => this.Topic.DateCreatedString; 

    public bool Busy 
    { 
     get { return _busy; } 
     set { SetProperty(ref _busy, value); } 
    } 

    public long ForumId 
    { 
     get { return _forumId; } 
     set {SetProperty(ref _forumId, value); } 
    } 

    public long TopicId 
    { 
     get { return _topicId; } 
     set { SetProperty(ref _topicId, value); } 
    } 

    public void Init(long TopicId, long ForumId) 
    { 
     this.TopicId = TopicId; 
     this.ForumId = ForumId; 
    } 
} 

和我有Android的XML视图,像这样

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"> 
     <LinearLayout 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent"> 
      <MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:MvxBind="Refreshing Busy; RefreshCommand LoadReplyCommand"> 
       <MvxRecyclerView 
        android:scrollbars="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        app:MvxItemTemplate="@layout/listitem_commentlist" 
        app:MvxBind="ItemsSource Comments;" /> 
      </MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout> 
      <View 
       android:layout_width="match_parent" 
       android:layout_height="4px" 
       android:background="@color/grey_soft" /> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent"> 
      <android.support.v7.widget.CardView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="5dp" 
       app:cardCornerRadius="@dimen/spacing_medium" 
       app:cardElevation="@dimen/spacing_xsmall" 
       app:cardUseCompatPadding="true"> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical"> 
        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="@dimen/spacing_medium" 
         android:layout_marginLeft="@dimen/spacing_large" 
         android:layout_marginRight="@dimen/spacing_large" 
         android:layout_marginTop="@dimen/spacing_large" 
         android:textAppearance="@style/TextAppearance.AppCompat.Headline" 
         android:textColor="@color/upload_photo_button_color" 
         app:MvxBind="Text Heading" /> 
        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_margin="@dimen/spacing_large" 
         android:text="@string/lorem_ipsum" 
         app:MvxBind="Text Description" 
         android:textAppearance="@style/TextAppearance.AppCompat.Body1" 
         android:textColor="@color/grey_dark" /> 
        <View 
         android:layout_width="match_parent" 
         android:layout_height="2px" 
         android:background="@color/grey_soft" /> 
        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_margin="@dimen/spacing_large" 
         app:MvxBind="Text DateCreatedString" 
         android:textAppearance="@style/TextAppearance.AppCompat.Body1" 
         android:textColor="@color/upload_photo_button_color" 
         android:textStyle="italic" /> 
       </LinearLayout> 
      </android.support.v7.widget.CardView> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

问题:

MvxRecyclerView的内容,虽然我的收藏确信是从来没有显示在构造函数中有项目。

其他每个绑定按预期工作。如果我注释掉除MvxRecyclerView以外的所有内容,它就会变得可见。

请我想我做错了什么。

有人能指出我正确的方向吗?

谢谢。

回答

0

您的LinearLayout似乎没有一个方向设置。我认为默认的设置为Horizontal,这意味着您的布局从左向右流动并超出屏幕的可见区域。

另外一个问题是,你的布局是非常嵌套的,这意味着它可能会有大量的透支和额外的测量过程,当它要在屏幕上绘制时。您可以使用一个RelativeLayout而不是4个嵌套层LinearLayout来取消嵌套。

像这样的东西可能会奏效:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout 
     android:layout_alignParentTop="true" 
     android:layout_above="@+id/divider" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:MvxBind="Refreshing Busy; RefreshCommand LoadReplyCommand"> 
     <MvxRecyclerView 
      android:scrollbars="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:MvxItemTemplate="@layout/listitem_commentlist" 
      app:MvxBind="ItemsSource Comments" /> 
    </MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout> 
    <View 
     android:id="@+id/divider" 
     android:layout_above="@+id/heading_card" 
     android:layout_width="match_parent" 
     android:layout_height="4px" 
     android:background="@color/grey_soft" /> 
    <android.support.v7.widget.CardView 
     android:id="@+id/heading_card" 
     android:layout_alignParentBottom="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     app:cardCornerRadius="@dimen/spacing_medium" 
     app:cardElevation="@dimen/spacing_xsmall" 
     app:cardUseCompatPadding="true"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="@dimen/spacing_medium" 
       android:layout_marginLeft="@dimen/spacing_large" 
       android:layout_marginRight="@dimen/spacing_large" 
       android:layout_marginTop="@dimen/spacing_large" 
       android:textAppearance="@style/TextAppearance.AppCompat.Headline" 
       android:textColor="@color/upload_photo_button_color" 
       app:MvxBind="Text Heading" /> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="@dimen/spacing_large" 
       android:text="@string/lorem_ipsum" 
       app:MvxBind="Text Description" 
       android:textAppearance="@style/TextAppearance.AppCompat.Body1" 
       android:textColor="@color/grey_dark" /> 
      <View 
       android:layout_width="match_parent" 
       android:layout_height="2px" 
       android:background="@color/grey_soft" /> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="@dimen/spacing_large" 
       app:MvxBind="Text DateCreatedString" 
       android:textAppearance="@style/TextAppearance.AppCompat.Body1" 
       android:textColor="@color/upload_photo_button_color" 
       android:textStyle="italic" /> 
     </LinearLayout> 
    </android.support.v7.widget.CardView> 
</RelativeLayout>