ListView 自定义适配器对错误的项目 OnScroll 应用操作

ListView Custom Adapter applies actions on wrong Items OnScroll

我有 ListView 和自定义 ArrayAdapter 的正常设置。 我规定如果某物等于某物然后改变该项目的颜色。

在第一次加载时,条件适用于我更改其颜色(如果条件)的正确项目。

当我连续向上和向下滚动 ListView 时,问题开始出现,错误项目的颜色随机变化,并随着我不断滚动而恢复到原来的颜色。即使是正确的也会随机变化。直到我将列表中的所有项目都设置为该颜色!

假设只有一项具有 (true) 值,其余为 (false) 那么我条件 如果为真,改变颜色。但是当我滚动时(不是在第一次加载时)其他项目即使是 false.

也会得到那种颜色

但是我用 disabledTextView.setText("correct item"); 设置的数据不会改变,因为它是正确的,这很好。

MyCustomAdapter.java

public class MyCustomAdapter extends ArrayAdapter<HashMap<String, String>> {

    private Context context;
    private ArrayList<HashMap<String, String>> myArray;

    public MyCustomAdapter(Context context, ArrayList<HashMap<String, String>> theArray) {
        super(context, R.layout.my_single_list_item, theArray);

        this.context = context;
        this.myArray = theArray;
    }

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

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_single_list_item, parent, false);
        }

        HashMap<String, String> arrayItem = myArray.get(position);

        boolean disabled = Boolean.valueOf(arrayItem.get("disabled"));

        TextView disabledTextView = convertView.findViewById(R.id.disabledTextView);

        disabledTextView.setText(String.valueOf(disabled));

        if(disabled) {
            disabledTextView.setTextColor(getContext().getResources().getColor(R.color.design_default_color_error));
        }

        return convertView;
    }

}

my_single_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/disabledTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textColor="@android:color/black" />

</LinearLayout>

导致此问题的原因是什么?

更新(已解决):但仍需要更多信息!

在摆脱使用 convertView 并将其替换为 customView 和新的 LayoutInflater 之后:

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

    View customView = LayoutInflater.from(getContext()).inflate(R.layout.my_single_list_item, parent, false);
}

TextView hotspotUserDisabled = customView.findViewById(R.id.hotspotUserDisabled);

//.. etc

问题已解决。我仍然对此感到困惑,我是否应该仅在 View convertViewnull 时才使用 new View inflate?

因为 IDE 显示提示 :

When implementing a view Adapter, you should avoid unconditionally inflating a new layout; if an available item is passed in for reuse, you should try to use that one instead. This helps make for example ListView scrolling much smoother.

如果我需要修复此问题并仍然使用传递的 View 来保持滚动更顺畅怎么办?

谢谢!

您需要在 getView 方法中定义 else case

if(disabled) {
    disabledTextView.setTextColor(getContext().
    getResources().getColor(R.color.design_default_color_error));
}else{
    // add code here
}