Android 使用(StateListDrawable)与StateListDrawable

Android 使用(StateListDrawable)与StateListDrawable,android,xamarin,Android,Xamarin,我需要给一个背景色和每行动态按下的颜色。 两种方法中哪一种是最好的?哪一个性能更好 public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { var holder = viewHolder as adpCategoryPreviewViewHolder; using (StateListDrawable states = new StateListDrawa

我需要给一个背景色和每行动态按下的颜色。 两种方法中哪一种是最好的?哪一个性能更好

public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)
{
    var holder = viewHolder as adpCategoryPreviewViewHolder;

    using (StateListDrawable states = new StateListDrawable())
    {
    
        if (position % 2 == 0)
        {
            states.AddState(new int[] { Android.Resource.Attribute.StatePressed }, new ColorDrawable(Color.ParseColor("#ffc27c")));
            states.AddState(new int[] { }, new ColorDrawable(Color.ParseColor("#FFFFFF")));
        }
        else
        {
            states.AddState(new int[] { Android.Resource.Attribute.StatePressed }, new ColorDrawable(Color.ParseColor("#ffc27c")));
            states.AddState(new int[] { }, new ColorDrawable(Color.ParseColor("#bfddff")));
        }
        holder.layout.Background = (states);
    }
}
##VS


CPU性能在这两个示例中没有区别。唯一不同的是,使用模式的第一个示例所做的是,当代码超出使用的
范围时,处理
StateListDrawable
托管实例

这将使Android端在释放资源时摆脱其分配的内存,因为它是一个托管的可调用包装器

我更喜欢使用模式,因为它在内存泄漏方面更安全。但是,在速度方面,您不会看到任何性能提升

StateListDrawable states = new StateListDrawable();
if (position % 2 == 0)
{
    states.AddState(new int[] { Android.Resource.Attribute.StatePressed }, new ColorDrawable(Color.ParseColor("#ffc27c")));
    states.AddState(new int[] { }, new ColorDrawable(Color.ParseColor("#FFFFFF")));
}
else
{
    states.AddState(new int[] { Android.Resource.Attribute.StatePressed }, new ColorDrawable(Color.ParseColor("#ffc27c")));
    states.AddState(new int[] { }, new ColorDrawable(Color.ParseColor("#bfddff")));
}
holder.layout.Background = (states);