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

Posted

技术标签:

【中文标题】ListView 自定义适配器对错误的项 OnScroll 应用操作【英文标题】:ListView Custom Adapter applies actions on wrong Items OnScroll 【发布时间】:2021-01-21 20:59:00 【问题描述】:

我有一个ListView 和自定义ArrayAdapter 的正常设置。 我规定如果某物等于某物,则更改该项目的颜色。

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

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

假设只有一项具有 (true) 值,其余为 (false) 然后我条件 如果为 true,则更改颜色。但是当我滚动(不是第一次加载)时,其他项目即使是 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_
    android:layout_>

    <TextView
        android:id="@+id/disabledTextView"
        android:layout_
        android:layout_
        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 显示了提示:

当实现一个视图适配器时,你应该避免无条件地膨胀一个新的布局; 如果一个可用的项目被传入以供重复使用,您应该尝试使用该项目。 例如,这有助于使 ListView 滚动更加顺畅。

如果我需要解决这个问题并仍然使用传递的View 来保持滚动更流畅怎么办?

谢谢!

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

以上是关于ListView 自定义适配器对错误的项 OnScroll 应用操作的主要内容,如果未能解决你的问题,请参考以下文章

android ListView使用适配器,怎么让指定的项居中?

Android HttpUrlConnection Post 方法 - Json - Laravel API 对错误的响应

为 listview 定义 onlick:在 listView 级别的 Onclick 与自定义视图适配器内的 Onclick

ListView 的自定义适配器忽略设置值

使用自定义(对象)适配器过滤 ListView

具有包含 CheckBoxes 的自定义适配器的 Listview