在我的应用中,我必须根据列表视图项目的字符串内容自动设置ListView
项目的背景颜色。
当前,该应用正在使用以下代码:
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);
}
}
其中listViewAdapter
是MainActivity
类中的全局变量,如下所述,该变量在onCreate
方法中定义:
listViewAdapter= new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, settingItems);
listView.Adapter = listViewAdapter;
和settingItems
只是定义为字符串列表的全局变量。
上面的代码工作正常,因此给定列表视图项的背景色变为绿色,而列表视图项的其余部分变为默认颜色,直到列表视图向其添加垂直滚动。
现在,列表视图中有更多项并且一直在垂直滚动
View child = listView.GetChildAt(itemListPosition);
上述调用中的 child
对象为null。因此,我无法再根据列表位置访问列表视图中的项目。您知道我该如何解决吗?
答案 0 :(得分:0)
我用GetView
方法编写了一个演示,设置了项目背景色,您可以参考它。
这是演示的GIF。
您可以在列表视图适配器的GetView
方法中更改代码。我不知道要在列表视图中更改哪些项目,因此将位置设置为1,3,5。您可以自己更改。
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.axml
和ItemView.axml
,您可以通过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相关
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;
}
}