简体   繁体   English

为什么我的列表视图在滚动时会更改项目背景颜色?

[英]Why my listview change item background color while scrolling?

I use BaseAdapter for my listview.我将 BaseAdapter 用于我的列表视图。 I have following code:我有以下代码:

public class ListViewAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<JSONObject> arrayList;
private GetterSetter getterSetter = new GetterSetter();

public ListViewAdapter(Context context, ArrayList arrayList, Bundle bundle) {
    this.mContext = context;
    this.arrayList = arrayList;

    this.bundle = bundle;
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

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

    if (convertView == null) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        convertView = layoutInflater.inflate(R.layout.list_item, null);
    }

    TextView nameTextView = convertView.findViewById(R.id.textview_book_name);
    LinearLayout linearLayout = convertView.findViewById(R.id.item);

    JSONObject jsonObject = arrayList.get(position);

    try {

        String title = jsonObject.getString("title");
        String _id = jsonObject.getString("_id");

        nameTextView.setText(title);

        if(getterSetter.ifExists(_id)){
            linearLayout.setBackgroundColor(Color.parseColor("#397EAD"));
            nameTextView.setTextColor(Color.WHITE);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return convertView;
}

}

ifExists method in GetterSetter Class: GetterSetter 类中的 ifExists 方法:

public Boolean ifExists(String _id){

    if (myList != null) {
        for (int i=0;i<myList.size();i++){

            try {

                JSONObject jsonObject = myList.get(i);

                if(_id.equals(jsonObject.getString("_id"))){
                    return true;
                }

            } catch (JSONException e){}

        }
    }

    return false;

}

Layout for item:项目布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/item" >

<TextView
    android:id="@+id/textview_book_name"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="35dp"
    android:textSize="18sp"
    android:textColor="#000000" />

My listview work well but if I scrolling it, each item changing color suddenly.我的列表视图运行良好,但如果我滚动它,每个项目都会突然改变颜色。 I want that items have a RGB Color: #397EAD if his _id exists in static ArrayList in another class.如果他的 _id 存在于另一个类的静态 ArrayList 中,我希望这些项目具有 RGB 颜色:#397EAD。 How can I do that?我怎样才能做到这一点? How can I resolve a problem with changing color when I scrolling?如何解决滚动时更改颜色的问题?

在 xml 文件中设置背景颜色android:background="#397EAD" ,如果没有帮助在 ListView xml 中设置相同的行

You're not handling convertView recycling.您没有处理convertView回收。 You're setting the colors if it exists in the list, but you need an else block to set them back to the defaults if it doesn't exist in the list:如果列表中存在颜色,则您正在设置颜色,但如果列表中不存在,您需要一个else块将它们设置回默认值:

if(getterSetter.ifExists(_id)){
    linearLayout.setBackgroundColor(Color.parseColor("#397EAD"));
    nameTextView.setTextColor(Color.WHITE);
} else {
    // reset linearLayout and nameTextView colors to default here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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