Listiview Scrolling Down-Up改变了行的颜色

时间:2018-01-17 15:42:45

标签: java c# xamarin

我使用带有适配器的listview来根据值Active更改每行中的背景颜色。 此外,我在我的适配器中使用了支架,但每次我向下滚动我的列表视图时,所有颜色都已更改。

这是我的班级:

<f:link.action action="show" arguments="{operation:operation}" pageUid="{settings.operationSinglePid}">
<table>
<tr>
<td width="70px">[{operation.number}]</td>
<td width="170px"> <f:if condition="{operation.begin}">
                                {operation.begin -> f:format.date(format:'d.m.Y, H:i \U\h\r')}
                        </f:if></td>
<td width="260px">Stichwort:


<f:for each="{operation.type}" as="stichwort">
{stichwort.title}
</f:for>





</td>
<td width="300px"> <f:if condition="{operation.location}">
                        <span>{operation.location}</span>
                </f:if>
</td>
</tr>
</table>
</f:link.action>

这是我的适配器:

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; }

}

TextView txtActive负责我的行采用哪种颜色。 示例如果txtActive = 1行为绿色,则txtActive = 2行为橙色

Image Before scrolling listview

Image after Scolling Down and up again my listview

另一行自动改变了颜色。

1 个答案:

答案 0 :(得分:0)

我过去只使用了多个持有者的ListViews,但我认为你的GetView持有人模式并不合适。我认为应该看起来更像这样。

    override View GetView(int position, View convertView, ViewGroup parent)
    {
        DataViewHolder holder = null;

        var view = convertView;
        if(view != null)
        {
            holder = view.Tag as DataViewHolder;
        }

        if (holder == null)
        {
            view = LayoutInflater.From(mContext).Inflate(Resource.Layout.FactAdapter, null, false);
            holder = new DataViewHolder();

            holder.txtid = view.FindViewById<TextView>(Resource.Id.idtxt);
            holder.txtName = view.FindViewById<TextView>(Resource.Id.Nametxt);
            holder.txtPhone = view.FindViewById<TextView>(Resource.Id.Phonetxt);
            holder.txtActive = view.FindViewById<TextView>(Resource.Id.Activetxt);

            view.Tag = holder;
        }

        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);
        }
        else if (holder.txtActive.Text == "2")
        {
            holder.txtName.SetBackgroundColor(Color.Orange);
            holder.txtPhone.SetBackgroundColor(Color.Orange);
        }
        else
        {
           holder.txtName.SetBackgroundColor(Color.White);
           holder.txtPhone.SetBackgroundColor(Color.White);
        }

        return view;
    }

此外,这里有一个很棒的blog post,其中包含有关Android ListViews和持有者的更多信息。