【发布时间】:2018-01-17 15:42:45
【问题描述】:
我正在使用一个带有适配器的列表视图,用于根据 Active 值更改每一行的背景颜色。 我也在我的适配器中使用了支架,但是每次我向下和向上滚动我的列表视图时,所有颜色都被更改了。
这是我的班级:
class FactClass
{
[PrimaryKey, AutoIncrement, Column("_Id")]
public int id { get; set; } // AutoIncrement and set primarykey
public string Name { get; set; }
public string Phone { get; set; }
public string Comments { get; set; }
public int Active { get; set; }//According this Values ListView is changing Color
public string Location { get; set; }
public bool IsChecked { get; set; }
}
这是我的适配器:
public override View GetView(int position, View convertView, ViewGroup parent)
{
DataViewHolder holder = null;
if (convertView == null)
{
convertView = LayoutInflater.From(mContext).Inflate(Resource.Layout.FactAdapter, null, false);
holder = new DataViewHolder();
holder.txtid = convertView.FindViewById<TextView>(Resource.Id.idtxt);
holder.txtName = convertView.FindViewById<TextView>(Resource.Id.Nametxt);
holder.txtPhone = convertView.FindViewById<TextView>(Resource.Id.Phonetxt);
holder.txtActive = convertView.FindViewById<TextView>(Resource.Id.Activetxt);
convertView.Tag = holder;
}
else
{
holder = convertView.Tag as DataViewHolder;
}
holder.txtid.Text = Convert.ToString(mitems[position].id);
holder.txtName.Text = mitems[position].Name;
holder.txtPhone.Text = mitems[position].Phone;
holder.txtActive.Text = Convert.ToString(mitems[position].Active);
if (holder.txtActive.Text == "1")
{
holder.txtName.SetBackgroundColor(Color.LightGreen);
holder.txtPhone.SetBackgroundColor(Color.LightGreen);
}
if (holder.txtActive.Text == "2")
{
holder.txtName.SetBackgroundColor(Color.Orange);
holder.txtPhone.SetBackgroundColor(Color.Orange);
}
return convertView;
}
public class DataViewHolder : Java.Lang.Object
{
public TextView txtid { get; set; }
public TextView txtName { get; set; }
public TextView txtPhone { get; set; }
public TextView txtActive { get; set; }
}
TextView txtActive 负责哪个颜色将占据我的行。 示例如果 txtActive =1 行是绿色, txtActive =2 行是橙色
Image Before scrolling listview
Image after Scolling Down and up again my listview
另一行自动改变了他的颜色。
【问题讨论】:
-
我认为您的
if (holder.txtActive.Text == "2")应该是else if,如果您的holder.txtActive.Text不是 1 或 2,您是否也需要 else 案例。 -
相同的结果。我认为我需要在 txtActive 中创建标签(在我的情况下)。然后在 Tag!=null ......我的代码的其余部分使用它。