0

如果文本是 5 以下的数字,我正在尝试更改 listitem 中文本视图的文本颜色。但是当我向上/向下滚动时,颜色适用于回收项目。我环顾四周,我真的不明白这些回收物品是如何工作的。

片段类

ListAdapter adapter = new RacesAdapter(getActivity(), racesList,
            R.layout.list_item,
            new String[] { TAG_NAME, TAG_GOAL, TAG_NUMENTRANTS }, new int[] {
                    R.id.gameLabel, R.id.goalLabel, R.id.entrantsLabel});
    setListAdapter(adapter);

RacesAdapter 类(是修改后的 SimpleAdapter)(在 bindView() 中查找我的代码)

public View getView(int position, View convertView, ViewGroup parent) {
    return createViewFromResource(position, convertView, parent, mResource);
}

private View createViewFromResource(int position, View convertView,
        ViewGroup parent, int resource) {

    View v;
    if (convertView == null) {
        v = mInflater.inflate(resource, parent, false);
    } else {
        v = convertView;
    }

    bindView(position, v);
    return v;
}

public void setDropDownViewResource(int resource) {
    this.mDropDownResource = resource;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    return createViewFromResource(position, convertView, parent, mDropDownResource);
}

@SuppressWarnings("rawtypes")
private void bindView(int position, View view) {
    final Map dataSet = mData.get(position);
    if (dataSet == null) {
        return;
    }

    final String[] from = mFrom;
    final int[] to = mTo;
    final int len = to.length;

    for (int i = 0; i < len; i++) {
        final View v = view.findViewById(to[i]);
        if (v != null) {


            final Object data = dataSet.get(from[i]);
            String text = data == null ? "" : data.toString();
            if (text == null) {
                text = "";
            }


            boolean bound = false;
            if (mViewBinder != null) {
                bound = mViewBinder.setViewValue(v, data, text);
            }



            if (!bound) {
                if (v instanceof TextView) {
                    if(v.getId()==R.id.entrantsLabel){
                        if(Integer.parseInt(text) < 5){((TextView) v).setTextColor(Color.RED);}
                    }
                    setViewText((TextView) v, text);
                } 
                else if (v instanceof ImageView) {
                    if (data instanceof Integer) {
                        setViewImage((ImageView) v, (Integer) data);                            
                    } else {
                        setViewImage((ImageView) v, text);
                    }
                } else {
                    throw new IllegalStateException(v.getClass().getName() + " is not a " +
                            " view that can be bounds by this SimpleAdapter");
                }
            }
        }
    }
}


public ViewBinder getViewBinder() {
    return mViewBinder;
}
4

1 回答 1

0

您应该做的是,在 中bindView(),将您的文本颜色设置为默认值。然后,它可能会或可能不会更改为Color.RED. 如果是,则为红色,否则为默认值。

所以,像这样:

private void bindView(int position, View view) {
    // ...

    for (int i = 0; i < len; i++) {
        final View v = view.findViewById(to[i]);
        if (v != null) {

            v.setTextColor(Color.BLACK); // For example
            // ...

            if (!bound) {
                if (v instanceof TextView) {
                    if(v.getId()==R.id.entrantsLabel){
                        if(Integer.parseInt(text) < 5){((TextView) v).setTextColor(Color.RED);}
                    }
                    // ...
                } 
                // ...
            }
        }
    }
}
于 2013-02-17T20:54:26.400 回答