java - ListView 向下滚动更改行颜色

标签 java c# xamarin

我正在使用内部带有适配器的 ListView ,用于根据 Activity 值更改每行的背景色。 另外,我在适配器中使用支架,但每次我向下和向上滚动 ListView 时,所有颜色都会发生变化。

这是我的类(class):

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

另一行已经自动改变了他的颜色。

最佳答案

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

关于java - ListView 向下滚动更改行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48304860/

相关文章:

java - Do/While Loop 当条件设置为 false 时循环

java - OkHttp 与 GzipRequestInterceptor 导致来自 Jetty 服务器的 400 Bad Request

c# - 如何为此类DataGrid组织ItemSsource

c# - 让 Akka.NET 连接到远程地址

c# - 在linq查询返回匿名类型中将可为空的int转换为int

xamarin.forms 如何使用 Entry 选择焦点上的所有文本?

ios - Xamarin、C++、Cocos2D-X 和 Visual Studio

java - 将字符串从 Java 传递到 Javascript 函数

java - 关于SOLR文档的问题等等

ios - 无法在指定的 director xamarin ios app 找到应用程序