【发布时间】:2016-09-29 19:54:23
【问题描述】:
我有一个用于 iOS 和 android 的 Xamarin Forms 项目的自定义 ListView 和一个自定义 ViewCell。我创建了自定义 listView 以便我的列表可以交替颜色(这非常成功)。不幸的是,当我创建自定义 ListView 时,它停止突出显示所选单元格,这就是我创建自定义 ViewCells 的原因。
我在 android 方面遇到了麻烦。我可以在选择单元格时更改颜色。但是我不确定一旦选择了另一个单元格,如何将颜色改回来。
这是我的 CustomList 类...
public class CustomList : ListView
{
public CustomList (){}
protected override void SetupContent(Cell content, int index)
{
base.SetupContent (content, index);
var currentCell = content as ViewCell;
currentCell.View.BackgroundColor = index % 2 == 0 ? Color.FromRgb (235, 235, 235) : Color.FromRgb (255, 255, 255);
}
}
很简单,只是根据索引设置颜色。 (也许我可以在此处添加其他内容来处理选定的单元格?)
这是 CustomCell 类:
public class CustomCell : ViewCell
{
public const String isSelectedProperty = "IsSelected";
public CustomCell ()
{
}
}
在 iOS 渲染器中:
public class CustomCellRenderer : ViewCellRenderer
{
private UIView view;
public CustomCellRenderer ()
{
}
public override UITableViewCell GetCell (Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell (item, reusableCell, tv);
if (view == null) {
view = new UIView (cell.SelectedBackgroundView.Bounds);
view.Layer.BackgroundColor = UIColor.FromRGB (128, 204,255).CGColor;
}
cell.SelectedBackgroundView = view;
return cell;
}
}
和 android 渲染器:
public class CustomCellRenderer : ViewCellRenderer
{
public CustomCellRenderer ()
{
}
protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
{
var cell = base.GetCellCore (item, convertView, parent, context);
return cell;
}
protected override void OnCellPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnCellPropertyChanged (sender, e);
var customCell = sender as CustomCell;
if (e.PropertyName == CustomCell.isSelectedProperty) {
customCell.View.BackgroundColor = Color.FromRgb (128, 204, 255);
}
}
}
所以 iOS 部分可以正常工作。并且 android 部分“工作”,因为它选择了单元格的颜色,但颜色保持不变。我只希望选定的单元格具有不同的背景颜色。有任何想法吗?
【问题讨论】:
-
所以你的意思是说你有 3 种颜色。 2 表示交替列表项,1 表示选择其中任何一项?@Shane
-
@AkashAmin 是的,没错。 iOS 部分有效,但我找不到类似于
cell.SelectedBackgroundViewfor android 的方法。我为 android 所拥有的也适用,但它只是一起改变了单元格的颜色,并且在未选择时不会变回 -
也许this 所以答案可以为您指明正确的方向
标签: listview xamarin xamarin.forms