listview滚动更改项目视图的背景颜色(simpleadapter)

问题描述 投票:0回答:1

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

我的lisview项目:

<?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>

我的simpleadapter:

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;

}

}

和MainActivity:

public boolean coloringDone = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mainActivity = this;

  ...

 tagListVU.setAdapter(adapter);
    tagListVU.deferNotifyDataSetChanged();
    tagListVU.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            tagListVU.removeOnLayoutChangeListener(this);


               coloringDone = true;

        }
    });

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

android listview simpleadapter
1个回答
0
投票

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

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

}
© www.soinside.com 2019 - 2024. All rights reserved.