MvxAdapter GetView() is not called for MvxListview

162 Views Asked by At

I am new in Xamarin Android and so for MvvmCrosss. I want to bind alternate images to my MvxListView rows and I'm trying to use custom adapter for it. But anyhow I know that I am missing something so trying to find way for it.

My MvxListview code is as below -

<Mvx.MvxListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@null"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_above="@+id/llSubtotal"
android:cacheColorHint="@android:color/transparent"
local:MvxBind="ItemsSource Items;ItemClick ItemSelectedCommand"
local:MvxItemTemplate="@layout/listviewitem"
android:id="@+id/mvxLVCustomerItemList" />

In above code I have defined ItemsSource and Itemtemplate for my listview. For ItemsSoure I have used viewmodel as below -

 public class HomeViewModel : MvxViewModel
{
    private readonly IRetailService _retailService;
    private IList<RetailItem> _items;
    public HomeViewModel(IRetailService RetailService)
    {
        _retailService = RetailService;
    }

    public override void Start()
    {
        IsLoading = true;
        _retailService.GetFeedItems(OnGarfieldItems, OnError);
    }
    private void OnGarfieldItems(IList<RetailItem> list)
    {
        IsLoading = false;
        Items = list;
    }
     public IList<RetailItem> Items
    {
        get { return _items; }
        set { _items = value; RaisePropertyChanged(() => Items); }
    }
    public IMvxCommand ItemSelectedCommand => new MvxCommand<string>(DoSelectItem);

     private void DoSelectItem(string item)
    {            
    }
}

My Template code is as below -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
              android:id="@+id/lvItemTemplate"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/odd">
    <Mvx.MvxImageView
        android:layout_width="150dp"
        android:layout_height="fill_parent"
        android:id="@+id/ivItemImage"
        android:textSize="40dp"
        android:layout_gravity="center_horizontal|center"
        local:MvxBind="ImageUrl StripUrl" />
    <TextView
        android:layout_marginTop="20dp"
        android:layout_width="fill_parent"
        android:id="@+id/tvItemName"
        android:layout_height="fill_parent"
        local:MvxBind="Text Title" />
</LinearLayout>

And below is the adapter I am using -

public class CustomAdapter : MvxAdapter
{
     public CustomAdapter(Context context, IMvxAndroidBindingContext bindingContext) : base(context, bindingContext)
    {
    }
    protected override View GetView(int position, View convertView, ViewGroup parent, int templateId)
    {
        var v = base.GetView(position, convertView, parent);
        LayoutInflater inflaterRMH = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
        LinearLayout llTemplate = (LinearLayout)v.FindViewById(Resource.Id.lvItemTemplate);
        llTemplate.SetBackgroundResource(position % 2 == 0 ? Resource.Drawable.odd : Resource.Drawable.even);
        return v;
     }
}

As shown in above code I am trying to change background image of linearlayout of template applied in listview.

Activity is as below -

public class HomeView : MvxAppCompatActivity, IOnClickListener
{
    private ListView lvRMH;
    public HomeViewModel HomeViewModel
    {
        get { return (HomeViewModel)base.ViewModel; }
    }
    protected override void OnViewModelSet()
    {
        SetContentView(Resource.Layout.Home);
        lvRMH = (ListView)FindViewById(Resource.Id.mvxLVRMHList);
            lvRMH.Adapter = new CustomAdapter(this, (IMvxAndroidBindingContext)BindingContext);
    }
     protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
    }
}

I know that I am doing some terrible mistake about understanding of MvvmCross and Android like the which I think is that GetView() is called before binding of Itemsource is bind to Listview. But if it is so what is workaround for it. And if it is possible to achieve odd-even row in MvxListview without changing itemsource binding of Listview through ViewModel as given my code.

Any help will be highly appreciated.

Thanks

1

There are 1 best solutions below

0
Nadeem Shaikh On

Finally I am able to call GetView now. I am not exactly sure but I think the issue was related to threading because I saw below message in my output window -

Skipped 300 frames! The application may be doing too much work on its main thread.

I just searched for it. Then I tried to do debugging step by step from OnCreate method and after that my debugger went on GetView method. Yes, it was threading related issue because of which GetView method was not called. Actually my data for list was loading using HttpClient and it was taking time to get response. So it was simply thread related issue.

Thanks.