Android 滚动时 ListView 行颜色更改问题

Android ListView Row color change issue on scroll

我在带有 CheckBox 的列表视图中列出已安装的应用程序,我正在选择任何项目并将其发送以进行扫描,扫描后我试图更改正确更改的行颜色,但是当我滚动列表视图时颜色在很多地方都发生了变化下面的行,我只想更改那个位置而不是在滚动其他行时更改。

我在哪里更改 Activity 中的行颜色。

String[] separated = result.split(":");
        int position = Integer.parseInt(separated[1]);
        if (separated[0].equals("R")) {
            View v = listView.getChildAt(position);
            v.setBackgroundColor(Color.RED);
        } else {
            View v = listView.getChildAt(position);
            v.setBackgroundColor(Color.GREEN);
        }

我的 BaseAdapter

public class Installedapps extends BaseAdapter {

public List<CustomObject> list;
CustomObject object2;
boolean[] itemChecked;
Activity context;

public Installedapps(Activity context, List<CustomObject> arg1) {
    super();
    this.context = context;
    list = arg1;
    itemChecked = new boolean[list.size()];
    object2 = new CustomObject();
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public CustomObject getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public void remove(int position) {
    list.remove(position);

}

@Override
public View getView(final int position, View convertView,
        ViewGroup parent) {
    final ViewHolder holder;
    final CustomObject object = getItem(position);
    if (convertView == null) {
        holder = new ViewHolder();

        // TODO Auto-generated method stub
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.adapter_installed_apps,
                parent, false);

        holder.im = (ImageView) convertView
                .findViewById(R.id.allapps_image);
        holder.checkBox = (CheckBox) convertView
                .findViewById(R.id.btn_scan_allapps);
        holder.checkBox.setChecked(false);
        holder.md5 = (TextView)convertView.findViewById(R.id.md5TXT);
        holder.name = (TextView) convertView
                .findViewById(R.id.allapps_pacagename);
        convertView.setTag(holder);

    } else {

        holder = (ViewHolder) convertView.getTag();
    }

    final ApplicationInfo into = PackageUtil.getPackagePath(
            context, object.Aplicationname);

    String pacage = object.Aplicationname;
    String name = (String) into.loadLabel(context.getPackageManager());
    Drawable ICON = into.loadIcon(context.getPackageManager());         
    final File file = new File(into.publicSourceDir);
    holder.im.setImageDrawable(ICON);
    holder.name.setText(name);
    holder.md5.setText(file.toString());

    if (itemChecked[position])
           holder.checkBox.setChecked(true);
          else
           holder.checkBox.setChecked(false);

          holder.checkBox.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
            // TODO Auto-generated method stub
            if (holder.checkBox.isChecked()){
             itemChecked[position] = true;
             DetailMessageModel.getInstance().changeState(
                     file.toString() + ":" + Integer.toString(position)
                        );
            }
            else{
             itemChecked[position] = false;
             RemoveItemModel.getInstance().changeState(
                     file.toString() + ":" + Integer.toString(position)
                        );
            }
           }
          });

    return convertView;
}
}

class ViewHolder {
CheckBox checkBox;
TextView name, md5;
ImageView im;

}

您的应用程序依赖于 baseAdapter class 的编程行为。显然,这在这种情况下效果不佳。所以我建议制作一个自定义适配器。

调用适配器时,它会根据位置编号为每一行设置一个标记。您还可以使用 xml 设置每一行的视图。

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    Notes theRec = (Notes) getItem(position);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View row;

    row = inflater.inflate(R.layout.notes_row, null);
    TextView title = (TextView) row.findViewById(R.id.text1);
    TextView noteId = (TextView) row.findViewById(R.id.noteid);
    TextView category = (TextView) row.findViewById(R.id.category);
    //you can put your imageview here

接下来,您将行的位置编号指定为其标签:

row.setTag(String.valueOf(position));
return row;

在您的 activity 中找到子列表视图并设置其背景颜色:

View v = mylistview.findViewWithTag(String.valueOf(index)); 
v.setBackgroundResource(R.color.some_color);

不要更改 activity 中的行颜色。您必须在 getView() 内执行此操作。

你可以这样做,因为我使用了 6 种颜色并在列表视图中重复它们。 我希望它能帮助你。 我的BaseAdapter代码。

public class ListViewAdapterSubCategories extends BaseAdapter{
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    private int[] color_arr = new int[] {Color.parseColor("#00ADEE"), Color.parseColor("#24B5EC"), Color.parseColor("#42BCEA"), Color.parseColor("#62C2E6"), Color.parseColor("#86CAE3"), Color.parseColor("#A1CFE0")};
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapterSubCategories (Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView subCategory_name;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item_subcategory, parent, false);
        int colorPos = position % color_arr.length;
        itemView.setBackgroundColor(color_arr[colorPos]);
        // Get the position
        resultp = data.get(position);

        return itemView;
    }
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null) {
        convertView = mInflator.inflate(R.layout.notes_row, null);
        holder = new Holder();
        holder.title = (TextView) row.findViewById(R.id.text1);
        holder.noteId = (TextView) row.findViewById(R.id.noteid);
        holder.category = (TextView) row.findViewById(R.id.category);
        convertView.setTag(holder)
    } else {
        holder = (Holder) convertView.getTag();
    }
    //do whatever here.. set, get values

    holder.setBackgroundColor(color_arr[colorPos]);

    return convertView;
}

class Holder {
     TextView title;
     TextView noteId;
     TextView category;
}