使用mono droid和mvvmcross的自定义列表视图问题

时间:2013-07-09 10:58:00

标签: listview xamarin.android adapter mvvmcross

我有一个包含自定义列表项的列表视图。该列表填充了自定义适配器。在此适配器中,我将列表项的背景设置为StateListDrawable。我的问题是,当在我的设备上运行时(nexus 4),一些listview单元格显示为灰色,或者说看起来似乎是缺少的。在约束器中,我创建了在填充列表视图时使用的所需颜色和渐变。

以下代码是我的适配器;

namespace CmsApp.Droid.Controls
{
    using Android.Content;
    using Android.Graphics;
    using Android.Graphics.Drawables;
    using Android.Views;
    using Android.Widget;
    using Cirrious.MvvmCross.Binding.Android.Views;

    public class TableCellAdapter : MvxBindableListAdapter
    {
        private GradientDrawable _backgroundGradient;
        private Color _textColor;
        private ColorDrawable _hightlightColor;

        // //creating needed colors and drawables for listitems
        public TableCellAdapter(Context context, string topgradientcolor, string bottomgradientcolor, string textcolor, string highlightcolor, bool vertical, string alphagradient) : base(context)
        {

            if (!string.IsNullOrEmpty(topgradientcolor) && !string.IsNullOrEmpty(bottomgradientcolor))
            {
                GradientDrawable.Orientation orientation = vertical ? GradientDrawable.Orientation.LeftRight: GradientDrawable.Orientation.TopBottom ;

                // convert from decimal too hex c# has no api for converting - duh!!
                var hexValue = "ff";
                if (!string.IsNullOrEmpty(alphagradient))
                {
                    var alpha = float.Parse(alphagradient.Replace('.', ','));

                    if (alpha >= 0.0 && alpha <= 1.0)
                    {
                        var decValue = (int)(255.0 * alpha);
                        hexValue = decValue.ToString("X").ToLower();
                    }
                }

                // background gradien
                _backgroundGradient = new GradientDrawable(orientation,
                                                           new int[]
                                                               {//c0
                                                                   Color.ParseColor("#" +hexValue+ topgradientcolor),
                                                                   Color.ParseColor("#"+hexValue + bottomgradientcolor)
                                                               });
            }

            if (!string.IsNullOrEmpty(textcolor))
            {
                _textColor = Color.ParseColor("#" + textcolor);
            }

            if (!string.IsNullOrEmpty(highlightcolor))
            {
                _hightlightColor = new ColorDrawable(Color.ParseColor("#" + highlightcolor));
            }  
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            if (convertView != null)
            {
                return base.GetView(position, convertView, parent); ;

            }

            var view = base.GetView(position, convertView, parent);


            //sld is created for each listitem or else it will fail
            StateListDrawable sld = new StateListDrawable();

            if (_hightlightColor != null)
            {
                sld.AddState(new[] { Android.Resource.Attribute.StatePressed }, _hightlightColor);
            }

            if (_backgroundGradient != null)
            {

                sld.AddState(StateSet.WildCard.ToArray(), _backgroundGradient);


            }

            var ll = view.FindViewById<LinearLayout>(Resource.Id.tableViewItem);
            if (ll != null)
            {
                ll.SetBackgroundDrawable(sld);

            }

            var textview = view.FindViewById<TextView>(Resource.Id.title);
            if (textview != null)
            {
                textview.SetTextColor(_textColor);
            }

            return view;
        }


    }
}

以下是我的listview xml

  <cirrious.mvvmcross.binding.android.views.MvxBindableListView
      android:id="@+id/ListView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:cacheColorHint="@android:color/transparent"
      android:orientation="vertical"
      android:fastScrollEnabled="true"
      local:MvxBind="{'ItemsSource':{'Path':'TableCells'}}"
      local:MvxItemTemplate="@layout/listitem_table" />

  </LinearLayout>

以下是我的lisitem xml:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:local="http://schemas.android.com/apk/res/dk.appsfabrikken.appsfabrikken"
  android:id="@+id/tableViewItem"
  android:layout_width="fill_parent"
  android:layout_height="60dp"
  android:paddingBottom="1dp"

  local:MvxBind="{'Click':{'Path':'ShowCellDetails'}}">

  <CmsApp.Droid.Controls.UmbracoImageView
    android:id="@+id/viewImagePreview"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:scaleType="centerCrop"
    android:padding="5dp"
    local:MvxBind="{'ImageData':{'Path':'ImageIconData'},'HideImage':{'Path':'Hidden'}}" />

  <TextView
    android:layout_gravity="center_vertical"
    android:id="@+id/title"
    android:textStyle="bold"
    android:textColor="@color/table_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    local:MvxBind="{'Text':{'Path':'Title'}}" />

</LinearLayout>

此图像显示当滚动时,一个项目突然变为灰色,当滚动停止时,它会变回正确。 enter image description here

编辑: 我没有使用xml这样做,因为颜色和渐变的定义是动态的,来自web服务。此外,活动的硬件加速设置为false。

0 个答案:

没有答案