Android的跨MVVM V3 - 如何创建具有交替行颜色一个ListView [英] Android MVVM Cross v3 - How to Create a ListView with alternating row colours

查看:156
本文介绍了Android的跨MVVM V3 - 如何创建具有交替行颜色一个ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个具有为每一行的交替背景色一个ListView。

I'd like to create a ListView that has an alternating background colour for each row.

我是新来的一般Android开发,别提了Xamarin和MVVMCross,以及:)

I'm new to Android development in general, never mind Xamarin and MVVMCross as well :)

有在Android世界和世界Xamarin这样的一些例子,但我无法弄清楚如何在MVVM跨的方式设置起来。

There are some examples of doing this in the Android world and the Xamarin world, but I can't figure out how to set this up in an MVVM Cross way.

我的理解是,我需要在Android特定视图code来实现这一点,要做到这一点,我将需要设置一些适配器,可以编程设置,因为它们是每个列表项的背景颜色的ListView渲染。

My understanding is that I will need to implement this in the Android specific view code and to do that I will need to set some adapter on the ListView that can programatically set the background colour of each List item as they are rendered.

我发现这个例子:

<一个href=\"https://github.com/slodge/MvvmCross-Tutorials/tree/master/Working%20With%20Collections/Collections.Droid\" rel=\"nofollow\">https://github.com/slodge/MvvmCross-Tutorials/tree/master/Working%20With%20Collections/Collections.Droid

不过,一旦实施我发现没有下面的方法被称为我的 CustomAdapter:MvxAdapter 显示视图时:

However, once implemented I find that none of the following methods are called on my CustomAdapter : MvxAdapter when displaying the view:

GetBindableView,GetSimpleView,GetBindableView,GetView

我也发现这个没有答案的问题。

I also found this unanswered question

<一个href=\"http://stackoverflow.com/questions/17546487/issue-with-custom-listview-using-mono-droid-and-mvvmcross\">Issue自定义列表视图中使用单Droid和mvvmcross

其中采用 MvxBindableListAdapter 这似乎不再存在了。

Which uses MvxBindableListAdapter which seems no longer to exist.

任何人都可以点我在正确的方向?

Can anyone point me in the right direction?

下面是所涉及的各种code片段:

Here are the various code snippets involved:

TradeBookingResponseView.axml拥有此:

TradeBookingResponseView.axml owns this:

<Mvx.MvxListView
  android:id="@+id/TradeConfirmation"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  local:MvxBind="ItemsSource OpenTradeListItems"
  local:MvxItemTemplate="@layout/item_confirmation"
/>

里面的的LinearLayout

item_confirmation.axml是这样的:

item_confirmation.axml is this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/tradingStateIcon"
    xmlns:local="http://schemas.android.com/apk/res-auto">
    <TextView
      android:layout_weight="0.35"
      android:text="Field"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dip"
      android:id="@+id/tradeConfirmationField"
      local:MvxBind="Text Field" />
    <TextView
      android:layout_weight="0.65"
      android:text="Value"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:id="@+id/tradeConfirmationValue"
      local:MvxBind="Text Value"/>
</LinearLayout>

该TradeBookingResponseViewModel公开此

The TradeBookingResponseViewModel exposes this

public ObservableCollection<ConfirmationItem> OpenTradeListItems
{
    get
    {
        return openTradeListItems;
    }
    set
    {
        if (!Equals(value, openTradeListItems))
        {
            openTradeListItems = value;
            RaisePropertyChanged(() => OpenTradeListItems);
        }
    }
}

虽然有观点code是

While the view code is

public class TradeBookingResponseView : ViewBase
{
    public TradeBookingResponseView() : base(Resource.Layout.TradeBookingResponseView)
    {

    }

    protected override void OnViewModelSet()
    {
        SetContentView(Resource.Layout.TradeBookingResponseView);
        var list = FindViewById<ListView>(Resource.Id.TradeConfirmation);
        list.Adapter = new CustomAdapter(this, (IMvxAndroidBindingContext) BindingContext);
    }
}

public class CustomAdapter : MvxAdapter
{
    public CustomAdapter(Context context) : base(context)
    {
    }

    public CustomAdapter(Context context, IMvxAndroidBindingContext bindingContext) : base(context, bindingContext)
    {
    }

    protected override View GetBindableView(View convertView, object dataContext)
    {
        convertView.SetBackgroundColor(Color.Red);
        return base.GetBindableView(convertView, dataContext);
    }
}

其中, ViewBase 扩展 MvxActivity

和我设置红色只是为了看它是否被调用(事实并非如此)。根据更多的Andr​​oid-Y的例子我想使用查看GetView(INT位置,查看convertView,一个ViewGroup父)来看看该项目是在什么位置,所以我可以做% 2就可以了(这也是不叫)。

And I'm setting red just to see if it gets called (it doesn't). According to more Android-y examples I want to use View GetView(int position, View convertView, ViewGroup parent) to see what position the item is at, so I can do % 2 on it (this also is not called).

推荐答案

尝试基本方法后设置颜色 - 例如

Try setting the color after the base method - eg

public override GetView(int position, View convertView, ViewGroup parent) 
{
      var v = base.GetView(position, convertView, parent);
      v.SetBackgroundColor( I %2 == 0 ? Color.Red : Color.Blue);
      return v;
}

另外,您可以数据绑定的背景颜色,如果你想

Alternatively you could data-bind the background color if you wanted to

这篇关于Android的跨MVVM V3 - 如何创建具有交替行颜色一个ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆