Mvvmcross RecyclerView Binding - PullRequest
       5

Mvvmcross RecyclerView Binding

0 голосов
/ 04 января 2019

Я пытаюсь связать некоторые данные с mvvmcross recyclerView, но я столкнулся с проблемой ... Recyclerview всегда пуст. Это мой код.Я довольно уверен, что все тупые файлы верны

HomeView (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>

Ма 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);
    }
}

Я что-то пропустил?Любая помощь будет принята с благодарностью.

1 Ответ

0 голосов
/ 05 января 2019

В вашем фрагменте HomeView закомментирована следующая строка:

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

И, похоже, он использует предоставляемый системой LayoutInflater.

Чтобы MvvmCross распознал определенные вами привязкив файлах макета axml необходимо использовать MvxLayoutInflater с помощью метода расширения this.BindingInflate.

Попробуйте изменить код следующим образом:

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

...