我有一个包含自定义列表项的列表视图。该列表填充了自定义适配器。在这个适配器中,我将列表项的背景设置为 StateListDrawable。我的问题是,在我的设备(nexus 4)上运行时,某些列表视图单元格显示为灰色,或者似乎 statedrawable 不存在。在构造函数中,我创建了填充列表视图时使用的所需颜色和渐变。
下面的代码是我的适配器;
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;
}
}
}
以下是我的列表视图 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>
这张图显示,当滚动一个项目时突然变灰,当滚动停止时它又变回了正确的状态。
编辑:我没有使用 xml 执行此操作,因为颜色和渐变的定义是动态的并且来自 Web 服务。此外,活动的硬件加速设置为 false。