gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:55 25 4
gpt4 key购买 nike

我正在使用内部带有适配器的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com