列表视图滚动更改项目视图的背景色(简单适配器)

时间:2019-03-17 06:21:58

标签: android listview simpleadapter

我对列表视图和simpleadapter有问题。首次绑定listview simpleadapter时,我更改了某些行的视图的背景色。问题是当我滚动列表视图时,它会随机更改视图的背景颜色。我真的不明白这里发生了什么。我使用变量(coloringDone)检查listview是否已绑定,因此避免再次更改颜色(在simpleadapter getView方法中),并且在第一次{{1}时,在onLayoutChange方法中将此变量设置为true }已加载。我在listview之后的getView方法中放置了一个断点,如果它没有命中。

我的lisview项目:

coloringDone

我的简单适配器:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:orientation="horizontal">



    <com.toptoche.searchablespinnerlibrary.SearchableSpinner
        android:id="@+id/person_spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <TextView
        android:id="@+id/Receiver_Name"
        android:layout_width="190dip"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textAlignment="gravity" />

    <TextView
        android:id="@+id/Asset_Name"
        android:layout_width="190dip"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textAlignment="gravity" />


</LinearLayout>

}

和MainActivity:

public class AssetSimpleAdapter extends SimpleAdapter {
HashMap<String, String> map = new HashMap<String, String>();
public AssetSimpleAdapter(Context context, List<? extends Map<String, String>> data,
                          int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
    mContext = context;
}

Context mContext;

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = super.getView(position, convertView, parent);



    if (!((MainActivity) mainActivity).coloringDone && some other conditions) {


            ((TextView) ((LinearLayout) view).findViewById(R.id.Asset_Name)).setBackgroundColor(mContext.getResources().getColor(R.color.red));
            ((TextView) ((LinearLayout) view).findViewById(R.id.Receiver_Name)).setBackgroundColor(mContext.getResources().getColor(R.color.red));




    }


    return view;

}

编辑:通过随机更改颜色,我的意思是有些行变成红色,有些行变成白色

1 个答案:

答案 0 :(得分:0)

最后,我解决了这个问题。我将适配器更改为ArrayAdapter,但仍然存在相同的问题,因此问题并非来自适配器类型。通过在适配器的getView方法中放置else子句并将视图的背景颜色更改为白色解决了问题(不需要colorDone变量):

   if (some other conditions) {

    ((TextView) ((LinearLayout) view).findViewById(R.id.Asset_Name)).setBackgroundColor(mContext.getResources().getColor(R.color.red));
    ((TextView) ((LinearLayout) view).findViewById(R.id.Receiver_Name)).setBackgroundColor(mContext.getResources().getColor(R.color.red));

}
else{

    ((TextView) ((LinearLayout) view).findViewById(R.id.Asset_Name)).setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
    ((TextView) ((LinearLayout) view).findViewById(R.id.Receiver_Name)).setBackgroundColor(mContext.getResources().getColor(android.R.color.white));

}
相关问题