简体   繁体   中英

listview scrolling changes background color of item views (simpleadapter)

I have a problem with listview and simpleadapter . I change the background color of views of some rows when first-time listview simpleadapter get binded. the problem is when I scroll listview it changes the background color of views randomly. I really don't understand what's happening here. I use a variable(coloringDone) to check if listview binded already so avoid changing color again(in simpleadapter getView method) and I set this variable to true in onLayoutChange method when first-time listview loaded. I put a breakpoint in getView method after coloringDone if and it doesn't hit.

my lisview item:

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

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

}

}

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

        }
    });

Edit: by changing color randomly I mean some rows become red and some rows becomes white

Finally I solved the problem. I changed the adapter to ArrayAdapter but still same problem so problem wasn't from adapter type. Problem solved by putting else clause and change the background color of views to white in getView method of adapter(and no need coloringDone variable):

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM