以编程方式设置列表视图项的背景颜色 [英] Programmatically set the background color of a list view item

查看:28
本文介绍了以编程方式设置列表视图项的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我必须根据该列表视图项的字符串内容自动设置 ListView 项的背景颜色.

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

其中 listViewAdapterMainActivity 类中的一个全局变量,如下定义到 onCreate 方法中:

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;

settingItems 只是一个定义为字符串列表的全局变量.

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 对象为空.因此,我无法再根据它们的位置访问列表视图中的项目.你知道我该如何解决这个问题吗?

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?

推荐答案

我在GetView方法中写了一个demo,设置item的背景色,可以参考.

I wrote a demo in GetView method, set the item background color, you could refer to it.

这是演示的 GIF.

您可以在列表视图适配器的 GetView 方法中更改代码.我不知道你想在listview中更改哪些项目,所以我将位置设置在1、3、5.你可以自己改.

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;

    }
}

下面是activity_main.axmlItemView.axml,你可以通过ItemView自定义listview中的item

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>

以下代码与MainActivity.cs相关

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

这篇关于以编程方式设置列表视图项的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆