问题
In my app, I have to automatically set the background color of a ListView item, based on the string content of that list view item.
Currently, the app is using the below code:
private void UpdateListView(string itemFromList, ListView listView)
{
int itemListPosition = listViewAdapter.GetPosition(itemFromList);
listView.SetItemChecked(itemListPosition, true);
View child = listView.GetChildAt(itemListPosition);
if (child != null)
{
for (int i = 0; i < listViewAdapter.Count; i++)
{
View otherChild = listView.GetChildAt(i);
if (otherChild != null)
{
otherChild.SetBackgroundColor(defaultColor);
}
}
child.SetBackgroundColor(Color.Green);
}
}
where listViewAdapter is a global variable in the MainActivity class defined as below into the onCreate method:
listViewAdapter= new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, settingItems);
listView.Adapter = listViewAdapter;
and settingItems is just a global variable defined as a list of strings.
The above code was working fine, so the background color of a given list view item was changing to green and the rest of the list view items to the default color, until the list view added a vertical scroll to it.
Now after the list view got more items and a vertical scroll all the time the
View child = listView.GetChildAt(itemListPosition);
child object from the above call is null. Consequently, I can't access anymore the items from the list view based on their position. Do you know how can I work around this?
回答1:
I wrote a demo in GetView method, set the item background color, you could refer to it.
This is GIF of demo.
You could change code in the GetView method of listview adapter. I do not know which items you want to change in listview, so I set the position in 1,3,5. you could change it by yourself.
MyAdapter.cs
class MyAdapter : BaseAdapter<ColorDemo>
{
Activity context;
List<ColorDemo> colors { get; set; }
public MyAdapter(Activity context , List<ColorDemo> colors)
{
this.context = context;
this.colors = colors;
}
public override ColorDemo this[int position] {
get
{
return colors[position];
}
}
public override int Count => colors.Count;
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View newView = convertView;
if (newView == null)
newView = context.LayoutInflater.Inflate(Resource.Layout.ItemView, null);
newView.FindViewById<TextView>(Resource.Id.item_tv).Text = colors[position].ColorName;
if (position==1|| position == 3|| position == 5)
{
newView.FindViewById<TextView>(Resource.Id.item_tv).SetBackgroundColor( Color.Blue);
}
else
{
newView.FindViewById<TextView>(Resource.Id.item_tv).SetBackgroundColor(Color.Green);
}
return newView;
}
}
The following is activity_main.axml and ItemView.axml, you could customize items in the listview by ItemView
activity_main.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_color"
/>
</RelativeLayout>
ItemView.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this text"
android:textSize="50dp"
android:id="@+id/item_tv"
android:background="@android:color/holo_red_dark"
/>
</LinearLayout>
The following code is related to the MainActivity.cs
MainActivity.cs
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
ListView lv_color = FindViewById<ListView>(Resource.Id.lv_color);
// lv_color.SetItemChecked(2,true);
List<ColorDemo> colors = new List<ColorDemo>();
colors.Add(new ColorDemo("red"));
colors.Add(new ColorDemo("green"));
colors.Add(new ColorDemo("red"));
colors.Add(new ColorDemo("green"));
colors.Add(new ColorDemo("green"));
colors.Add(new ColorDemo("green"));
colors.Add(new ColorDemo("red"));
colors.Add(new ColorDemo("green"));
colors.Add(new ColorDemo("red"));
colors.Add(new ColorDemo("green"));
colors.Add(new ColorDemo("green"));
colors.Add(new ColorDemo("green"));
colors.Add(new ColorDemo("red"));
colors.Add(new ColorDemo("green"));
colors.Add(new ColorDemo("red"));
colors.Add(new ColorDemo("green"));
colors.Add(new ColorDemo("green"));
colors.Add(new ColorDemo("green"));
MyAdapter myAdapter = new MyAdapter(this,colors);
lv_color.Adapter=myAdapter;
}
}
来源:https://stackoverflow.com/questions/54011699/programmatically-set-the-background-color-of-a-list-view-item