0

我编写了一个自定义适配器(LevelAdapter因为我正在显示游戏级别的列表),它ListView从虚拟对象列表中填充片段。每个虚拟对象都拥有一个布尔“解锁”属性,我试图根据底层虚拟项目是否“解锁”有条件地禁用和设置列表中每个项目的一些样式属性。为此,我还雇用了一个ViewHolder.

下面的代码可以正常工作,直到用户向下滚动并向后滚动,此时“解锁”项目不会被禁用,但它们确实会失去适当的样式。

public class LevelAdapter extends ArrayAdapter<DummyContent.DummyItem> {
    private List<DummyContent.DummyItem> objects;
    private DummyContent.DummyItem item;
    private Context context;
    private int textViewResourceId;
    private int resource;

    public LevelAdapter(Context context, int resource, int textViewResourceId, List<DummyContent.DummyItem> objects) {
        super(context, resource, textViewResourceId, objects);
        this.objects = objects;
        this.context = context;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;

    }

    static class ViewHolder {
        TextView text;
        int position;
        boolean unlocked;
    }

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


        // https://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
        ViewHolder holder;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(resource, null);
            holder = new ViewHolder();
            holder.text = (TextView) v.findViewById(textViewResourceId);
            holder.position = position;
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        item = objects.get(position);
        holder.text.setText(item.title);

        if (item.unlocked == false) {
            holder.text.setTextColor(Color.LTGRAY);
            Typeface type = Typeface.create("", Typeface.ITALIC);
            holder.text.setTypeface(type);
        }

        return v;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return true; // technically untrue, but a hack to show the line divider between items
    }

    @Override
    public boolean isEnabled(int position) {

        item = objects.get(position);
        if (item.unlocked == true) {
            return true;
        } else {

            return false;
        }

    }

} 
4

1 回答 1

0
    if (item.unlocked == false) {
        holder.text.setTextColor(Color.LTGRAY);
        Typeface type = Typeface.create("", Typeface.ITALIC);
        holder.text.setTypeface(type);
    }

unlocked == true您应该为case创建 else 语句。

现在,如果适配器获取视图以重用具有未锁定 false 的项目来填充已解锁 true 的项目,则它不会更改样式但保留旧样式。

于 2013-07-15T01:37:36.900 回答