1

我创建了一个带有自定义适配器的 ListView,它扩展了 SimpleAdapter。第五个是粉红色的。当我上下滑动时,其他项目会随机获得与第五个相同的背景颜色。
我觉得很奇怪,这是为什么呢?

截图:
截图1:启动后的应用程序
截图2:我上下滑动几次后的应用程序

文件:
MainActivity.java:

package com.example.test;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListAdapter;

public class MainActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Car> cars = getData();
        ListAdapter adapter = new CarListAdapter(this, cars, android.R.layout.simple_list_item_2, new String[] {
                Car.KEY_MODEL, Car.KEY_MAKE }, new int[] { android.R.id.text1, android.R.id.text2 });
        this.setListAdapter(adapter);
    }

    private List<Car> getData() {
        List<Car> cars = new ArrayList<Car>();
        for(int i = 0; i < 15; i++) {
            cars.add(new Car("Car "+i, i+""));
        }
        return cars;
    }
}



CarListAdapter.java:

package com.example.test;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

public class CarListAdapter extends SimpleAdapter {
    private List<Car> cars;
    private int[] colors = new int[] { 0x30ff2020, 0x30ff2020, 0x30ff2020 };

    @SuppressWarnings("unchecked")
    public CarListAdapter(Context context,
            List<? extends Map<String, String>> cars, int resource,
            String[] from, int[] to) {
        super(context, cars, resource, from, to);
        this.cars = (List<Car>) cars;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        if (position == 5) {
            int colorPos = position % colors.length;
            view.setBackgroundColor(colors[colorPos]);
        }

        return view;
    }

}



汽车.java:

package com.example.test;

import java.util.HashMap;

public class Car extends HashMap<String, String> {
    private static final long serialVersionUID = 12872473L;
    public String make;
    public String model;

    public static String KEY_MAKE = "make";
    public static String KEY_MODEL = "model";

    public Car(String make, String model) {
        this.make = make;
        this.model = model;
    }

    @Override
    public String get(Object k) {
        String key = (String) k;
        if (KEY_MAKE.equals(key))
            return make;
        else if (KEY_MODEL.equals(key))
            return model;
        return null;
    }
}



活动主.xml:

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

    <ListView android:id="@android:id/list" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:visibility="visible"/>

</LinearLayout>
4

2 回答 2

0

在 getView 中,您没有处理其他情况(当它不等于 5 时):

  if (position == 5) {
            int colorPos = position % colors.length;
            view.setBackgroundColor(colors[colorPos]);
        }
  else view.setBackgroundColor(defaultColor);

原因是它使用旧视图来重用。

此外,您应该使用 android:cacheColorHint="#00000000" 。这是一篇关于它的文章:http ://www.curious-creature.org/2008/12/22/why-is-my-list-black-an-android-optimization/

于 2013-06-08T22:16:56.233 回答
0

这是因为您编写的适配器不正确,并且基本上在视图回收机制的帮助下创建了一些错误,您可以在此处阅读更多信息。删除这一行:

 View view = super.getView(position, convertView, parent);

而是写这个:

View row;
LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflator.inflate(R.layout.new_row, null);
if (position == 5) {
        int colorPos = position % colors.length;
        row .setBackgroundColor(colors[colorPos]);
}

return row ;

更新:

使用该getView方法时,您需要在 layouts 文件夹中为您的行创建一个布局,并用作每一行的布局。在运行期间getView(为您创建的每一行运行的方法),您可以根据列表中的特定行或附加到特定位置的列表项的其他数据ListView对特定行进行所需的更改,您可以这样做(position最好通过创建一个从s)ViewHolder的角度描述行的视觉方面的方法,如下所示:View

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    final ViewHolder holder = new ViewHolder();
    LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflator.inflate(R.layout.new_row, null);
    holder.tvId = (TextView) row.findViewById(R.id.text_id);
    holder.tvId.setText(position);
    ...

    if (position == 5) {
        int colorPos = position % colors.length;
        row .setBackgroundColor(colors[colorPos]);
    }
    return row ;
}

static class ViewHolder
{
  TextView tvId;
  TextView tvTitle;
}

UPDATE2: 我想正如评论中所说,这将是一个更好的选择:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflator.inflate(R.layout.list, null);
        holder.tvId = (TextView) convertView.findViewById(R.id.text_id);
        ...
        if (position == 5) {
           int colorPos = position % colors.length;
           convertView.setBackgroundColor(colors[colorPos]);
        }
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.tvId.setText(position);
    ...

    return convertView;
}

static class ViewHolder
{
  TextView tvId;
  TextView tvTitle;
}

但我发现这有问题并且并不总是正常工作。

于 2013-06-08T15:44:28.327 回答