【问题标题】:Mvvmcross RecyclerView BindingMvvmcross RecyclerView 绑定
【发布时间】:2019-01-03 23:15:04
【问题描述】:

我正在尝试使用 mvvmcross recyclerView 绑定一些数据,但我遇到了一个问题……Recyclerview 总是空的。这是我的代码。我很确定所有 stup 文件都是正确的

主页视图(xml 文件):

     <!-- language: xml -->
     <ScrollView
    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.V7.RecyclerView.MvxRecyclerView
            android:id="@+id/product_recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:scrollbars="vertical"
            app:MvxItemTemplate="@layout/product_item_row"
            app:MvxBind="ItemsSource Products"/>

      </ScrollView>

product_item_row:

    <!-- language: xml -->
    <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="match_parent">

        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_margin="@dimen/card_margin"
            android:clickable="true"
            android:elevation="3dp"
            android:foreground="?attr/selectableItemBackground">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="@dimen/card_cover_height"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:clickable="true"
    android:src="@drawable/ic_store_24"
            android:scaleType="fitXY" />

         <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/thumbnail"
            android:lines="2"
            android:paddingLeft="@dimen/card_name_padding"
            android:paddingRight="@dimen/card_name_padding"
            android:paddingTop="@dimen/card_name_padding"
            android:textColor="#111"
            android:textSize="11dp" 
        android:text="Name"
       app:MvxBind="Text Name"/>

        <TextView
            android:id="@+id/price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/name"
            android:layout_marginRight="10dp"
            android:gravity="right"
            android:paddingBottom="@dimen/card_price_padding_bottom"
            android:textColor="@color/colorAccent"
            android:textSize="11dp"
    android:text="Price"
    app:MvxBind="Text Price"/>

        </RelativeLayout>

        </android.support.v7.widget.CardView>

     </LinearLayout>

Ma viewModel(HomeViewModel):

<!-- language: c# -->
public class HomeViewModel : MvxViewModel
{
     private readonly ICarouselService _carouselService;
     private readonly IProductDataService _productDataService;
     private MvxObservableCollection<Carousel> _carousels;
     private MvxObservableCollection<Product> _products;

    public MvxObservableCollection<Carousel> Carousels
    {
        get { return _carousels; }
        set
        {
            _carousels = value;
            RaisePropertyChanged(() => Carousels);
        }
    }

    public MvxObservableCollection<Product> Products
    {
        get { return _products; }
        set
        {
            _products = value;
            RaisePropertyChanged(() => Products);
        }
    }

    public HomeViewModel(ICarouselService carouselService, IProductDataService productDataService)
    {
        _carouselService = carouselService;
        _productDataService = productDataService;
    }

    public override async Task Initialize()
    {
        Products = new MvxObservableCollection<Product>(await _productDataService.GetProducts());
        Carousels = new MvxObservableCollection<Carousel>(await _carouselService.GetImages());
    }
}

我的片段:

<!-- language: c# -->
[MvxFragmentPresentation(typeof(MainViewModel), Resource.Id.content_frame, false)]
public class HomeView : BaseFragment<HomeViewModel>, IImageListener
{
    protected override int FragmentId => Resource.Layout.HomeView;
    CarouselView _carouselView;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate(Resource.Layout.HomeView, container, false);
        //var view = this.BindingInflate(Resource.Layout.HomeView, container, false);

        _carouselView = view.FindViewById<CarouselView>(Resource.Id.carouselView);
        var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.product_recycler);


        _carouselView.PageCount = ViewModel.Carousels.Count;
        _carouselView.SetImageListener(this);

        if (recyclerView != null)
        {
            recyclerView.HasFixedSize = true;
            var layoutManager = new GridLayoutManager(Activity, 2, LinearLayoutManager.Vertical, false);
            recyclerView.SetLayoutManager(layoutManager);

        }

        return view;
    }

    public void SetImageForPosition(int position, ImageView imageView)
    {

        Picasso.With(Context).Load(ViewModel.Carousels[position].Image)
            //.Placeholder(Resource.Mipmap.ic_launcher)
            //.Error(Resource.Mipmap.ic_launcher)
            //.Resize(500, 300)
            .Fit()
            .Into(imageView);
    }
}

我错过了什么吗?任何帮助将不胜感激。

【问题讨论】:

    标签: c# xamarin mvvmcross


    【解决方案1】:

    在您的 HomeView 片段中,以下行被注释掉:

     var view = this.BindingInflate(Resource.Layout.HomeView, container, false);
    

    而且它似乎在使用系统提供的 LayoutInflater。

    为了让 MvvmCross 识别您在 axml 布局文件中定义的绑定,您需要通过 this.BindingInflate 扩展方法使用 MvxLayoutInflater。

    尝试修改您的代码:

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreateView(inflater, container, savedInstanceState);
        var view = this.BindingInflate(Resource.Layout.HomeView, null);
    
        _carouselView = view.FindViewById<CarouselView>(Resource.Id.carouselView);
        var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.product_recycler);
    
    
        _carouselView.PageCount = ViewModel.Carousels.Count;
        _carouselView.SetImageListener(this);
    
        if (recyclerView != null)
        {
            recyclerView.HasFixedSize = true;
            var layoutManager = new GridLayoutManager(Activity, 2, LinearLayoutManager.Vertical, false);
            recyclerView.SetLayoutManager(layoutManager);
    
        }
    
        return view;
    }
    

    更多上下文: https://stackoverflow.com/a/53894136/2754727

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-18
      • 2013-10-17
      • 2014-10-10
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多