【问题标题】:Listiview Scrolling Down-Up Changes row's colorListview 向下滚动更改行颜色
【发布时间】: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 ......我的代码的其余部分使用它。

标签: java c# xamarin


【解决方案1】:

我过去只使用过多个持有者的 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 ListView 和持有者的更多信息。

【讨论】:

    猜你喜欢
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 2012-04-09
    • 2013-02-27
    • 2013-03-27
    • 2021-03-23
    相关资源
    最近更新 更多