【问题标题】:Why my listview change item background color while scrolling?为什么我的列表视图在滚动时会更改项目背景颜色?
【发布时间】:2020-02-25 22:38:49
【问题描述】:

我将 BaseAdapter 用于我的列表视图。 我有以下代码:

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

}

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;

}

项目布局:

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

我的列表视图运行良好,但如果我滚动它,每个项目都会突然改变颜色。 如果他的 _id 存在于另一个类的静态 ArrayList 中,我希望这些项目具有 RGB 颜色:#397EAD。我怎样才能做到这一点?如何解决滚动时更改颜色的问题?

【问题讨论】:

    标签: android listview baseadapter


    【解决方案1】:

    您没有处理convertView 回收。如果列表中存在颜色,则您正在设置颜色,但如果列表中不存在,则需要 else 块将它们设置回默认值:

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

    【讨论】:

    • 我在 else 块中添加了默认背景颜色和文本颜色。非常感谢!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      • 2016-01-15
      相关资源
      最近更新 更多