【问题标题】:Gridview BackGround Color changing Color when Scrolling in AndroidGridview背景颜色在Android中滚动时改变颜色
【发布时间】:2026-01-12 06:35:01
【问题描述】:

GridView 滚动时项目颜色随机变化。

每个项目的颜色都基于 SQLite 数据库的状态,但是当我尝试多次滚动时,它会随机更改项目的颜色。有没有办法解决这个问题?这就是它的样子。

这里是代码

获取视图

private class ViewHolder{
    ImageView imageView, mPreviewCashCard;
    TextView txtName, txtPrice, txtSeriesNo;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT id,status FROM DarkMode");
    while (cursor.moveToNext()) {
        DarkModeStatus = cursor.getString(1);
    }

    View row = view;
    ViewHolder holder = new ViewHolder();

    if(row == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(layout, null);
        holder.txtName = (TextView) row.findViewById(R.id.txtName);
        holder.txtPrice = (TextView) row.findViewById(R.id.txtPrice);
        holder.txtSeriesNo = (TextView) row.findViewById(R.id.txtSeriesNumber);
        holder.imageView = (ImageView) row.findViewById(R.id.imgFood);
        holder.mPreviewCashCard = (ImageView) row.findViewById(R.id.imgId);
        row.setTag(holder);
    }
    else {
        holder = (ViewHolder) row.getTag();
    }
    Inventory inventory = inventoryList.get(position);
    holder.txtName.setText(inventory.getName());
    holder.txtPrice.setText(inventory.getPrice());
    holder.txtSeriesNo.setText(inventory.getSeriesNumber());
    int status = inventory.getStatus();
    if (status==0 && DarkModeStatus.matches("false")){
        row.setBackgroundColor(Color.parseColor("#FEF8DD"));
    }
    else if(status==0 && DarkModeStatus.matches("true")){
        row.setBackgroundColor(Color.parseColor("#282828"));
    }

    byte[] CashCardImage = inventory.getImage();
    byte[] idImage = inventory.getIdImage();
    if(CashCardImage.length > 1)
    {
        Bitmap bitmap = BitmapFactory.decodeByteArray(CashCardImage, 0, CashCardImage.length);
        holder.imageView.setImageBitmap(bitmap);
    }
    else{
        holder.imageView.setImageResource(R.drawable.ic_image);
    }
    if(idImage.length > 1)
    {
        Bitmap bitmap2 = BitmapFactory.decodeByteArray(idImage, 0, idImage.length);
        holder.mPreviewCashCard.setImageBitmap(bitmap2);
    }
    else{
        holder.mPreviewCashCard.setImageResource(R.drawable.ic_image);
    }
    return row;
}

【问题讨论】:

    标签: java android gridview background-color


    【解决方案1】:

    尝试在 if-else 块处理背景中添加 else 条件。

         if (status==0 && DarkModeStatus.matches("false")){
            row.setBackgroundColor(Color.parseColor("#FEF8DD"));
        }
        else if(status==0 && DarkModeStatus.matches("true")){
            row.setBackgroundColor(Color.parseColor("#282828"));
        }else {
            row.setBackgroundColor(Color.parseColor("#FFFFFF")); // white or whichever default background color you are using
        }
    

    据我了解,这可以解决问题。

    【讨论】:

    • 感谢您的回复,我之前尝试过但我不能这样做,因为我在我的应用程序上使用了Darkmode,else 语句基于应用程序颜色,因为这是我的静态方式只需将 else 语句保留为我的 XML 中的背景默认值。但即便如此,我确实使用了那个,我认为结果会一样,你知道吗?我只是被这个模块卡住了:(
    • 由于条件也是基于深色模式,你不能在白色和深色主题的值中创建一个颜色资源并将其链接到这里吗?在这种情况下,您无需手动检查是否启用了深色主题。有关更多详细信息,请参阅此proandroiddev.com/…
    • 感谢它现在有效!我只是根据 DarkMode 添加了一些条件