【问题标题】:Android ListView Row color change issue on scroll滚动时的Android ListView Row颜色更改问题
【发布时间】:2017-07-14 16:21:30
【问题描述】:

我正在使用 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;

}

【问题讨论】:

  • 尝试在您的适配器 getview 方法中实现行颜色逻辑。

标签: android listview android-listview


【解决方案1】:

您可以这样做,因为我使用了 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;
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您的应用依赖于 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;
      

      在您的活动中,您可以找到子列表视图并设置其背景颜色:

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

      【讨论】:

        【解决方案4】:
        @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;
        }
        

        【讨论】:

          猜你喜欢
          • 2012-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-22
          • 2011-05-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多