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

时间:2015-02-04 16:44:45

标签: android listview android-listview

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

我在活动中更改行颜色。

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;

}

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

你可以这样做,因为我使用了6种颜色并在listview中重复它们。 我希望它对你有所帮助。 我的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;
    }
}

答案 2 :(得分:0)

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

当你调用适配器时,你可以根据位置编号为每一行设置一个标签。您还可以使用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;

在您的活动中,您会找到listview子项并设置其背景颜色:

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

答案 3 :(得分:0)

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