使用mono droid和mvvmcross的自定义listview出现问题
我有一个包含自定义列表项的listview。该列表由自定义适配器填充。在这个适配器中,我将列表项的背景设置为StateListDrawable。我的问题是,当在我的设备(nexus 4)上运行时,一些listview单元格变灰,或者更确切地说,statedrawable似乎不存在。在Constructor中,我创建了填充listview时使用的所需颜色和渐变 在下面的代码中是我的适配器使用mono droid和mvvmcross的自定义listview出现问题,listview,xamarin.android,adapter,mvvmcross,Listview,Xamarin.android,Adapter,Mvvmcross,我有一个包含自定义列表项的listview。该列表由自定义适配器填充。在这个适配器中,我将列表项的背景设置为StateListDrawable。我的问题是,当在我的设备(nexus 4)上运行时,一些listview单元格变灰,或者更确切地说,statedrawable似乎不存在。在Constructor中,我创建了填充listview时使用的所需颜色和渐变 在下面的代码中是我的适配器 namespace CmsApp.Droid.Controls { using Android.Con
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;
}
}
}
名称空间CmsApp.Droid.Controls
{
使用Android.Content;
使用Android.Graphics;
使用Android.Graphics.Drawables;
使用Android.Views;
使用Android.Widget;
使用cirries.MvvmCross.Binding.Android.Views;
公共类TableCellAdapter:MvxBindableListAdapter
{
私人梯度可绘制的背景梯度;
私人色彩(textColor),;
私人彩色可拉丝(hightlightColor);;
////为listitems创建所需的颜色和绘图
公共TableCellAdapter(上下文上下文、字符串topgradientcolor、字符串bottomgradientcolor、字符串textcolor、字符串highlightcolor、布尔垂直、字符串alphagradient):基本(上下文)
{
如果(!string.IsNullOrEmpty(topgradientcolor)和&!string.IsNullOrEmpty(bottomgradientcolor))
{
GradientDrawable.Orientation=垂直?GradientDrawable.Orientation.LeftRight:GradientDrawable.Orientation.TopBottom;
//从十进制转换为十六进制c#没有用于转换的api-duh!!
var hexValue=“ff”;
如果(!string.IsNullOrEmpty(alphagradient))
{
var alpha=float.Parse(alphagradient.Replace('.',',');
如果(alpha>=0.0&&alpha
<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>
<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>