ListView项目在滑动时获取另一个的背景颜色

时间:2013-06-08 15:31:43

标签: android android-listview

我创建了一个带有自定义适配器的ListView,它扩展了SimpleAdapter。第五项是粉红色。当我向上和向下滑动时,其他项目会随机变成与第五项相同的背景颜色。
对我来说这似乎很奇怪,为什么会这样?

截图:
Screenshot 1:启动后直接启用应用程序 Screenshot 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;
    }

}



Car.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;
    }
}



activity_main.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>

2 个答案:

答案 0 :(得分:0)

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

 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对特定行进行所需的更改或者您执行此操作的特定位置附加到列表项的其他数据(最好通过创建一个ViewHolder来描述您的行的视觉方面,从View s)的立场来这样:

@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;
}

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

答案 1 :(得分: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/