android - 即使没有数据,ListView 类的备用背景颜色

标签 android listview

我想为我的自定义 ListView 类设置交替颜色。

代码如下:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListView;

   public class CustomListView extends ListView {
    private Paint   mPaint              = new Paint();
    private Paint   mPaintBackground    = new Paint();

    public CustomListView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint.setColor(Color.parseColor("#1A000000"));

    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        final int currentHeight = getMeasuredHeight();

        final View lastChild = getChildAt(getChildCount() - 1);
        if (lastChild == null)
            return;
        for (int i = 0; i < getChildCount(); i++) {
            if (getChildCount() % 2 == 0) {
                mPaintBackground.setColor(Color.WHITE);
            } else {
                mPaintBackground.setColor(Color.RED);
            }
        }

        final int lastChildBottom = lastChild.getBottom();

        final int lastChildHeight = lastChild.getMeasuredHeight();

        final int nrOfLines = (currentHeight - lastChildBottom) / lastChildHeight;

        Rect r = new Rect(0, lastChildBottom, getMeasuredWidth(), getMeasuredHeight());
        canvas.drawRect(r, mPaintBackground);
        canvas.drawLine(0, lastChildBottom, getMeasuredWidth(), lastChildBottom, mPaint);
        for (int i = 0; i < nrOfLines; i++) {
            canvas.drawLine(0, lastChildBottom + (i + 1) * lastChildHeight, getMeasuredWidth(), lastChildBottom + (i + 1) * lastChildHeight, mPaint);
        }
        return;
    }
    }

为了获得 ListView 的交替背景颜色,我使用了以下代码:

for (int i = 0; i < getChildCount(); i++) {
   if (getChildCount() % 2 == 0) {
      mPaintBackground.setColor(Color.WHITE);
   } else {
      mPaintBackground.setColor(Color.RED);
   }
}

适配器内部:

    if (position % 2 == 0) {
        view.setBackgroundColor(Color.RED);
    } else {
        view.setBackgroundColor(Color.WHITE);
    }

但它总是显示一种颜色,红色或白色与我尝试的一切。我没有得到白色-红色-白色-红色交替的颜色。

最佳答案

失败的原因是你的 for 循环永远不会改变。您总是在检查 getChildCount() % 2getChildCount() 将为每次迭代返回相同的值。您需要根据职位进行检查:

for(int i = 0; i < getChildCount(); i++){
   if(i % 2 == 0){
      mPaintBackground.setcolor(Color.WHITE);
   } else{
      mPaintBackground.setColor(Color.RED);
   }
}

如果有帮助,请将您的计数器变量从 i 重命名为 position 以便将来对您更具可读性,或者记下它以提供帮助自己出去。

我还想补充一点,鉴于您现在拥有的代码,您的 for 循环并没有改变任何东西。它只是遍历子元素的数量并设置 mPaintBackground。最后,它将保留从上次迭代中接收到的任何值。

我认为处理绘制背景颜色的最佳方法是在 Listview 的适配器中,在这种情况下您可以覆盖 getView()并根据 position 参数进行检查:

int backgroundResource;
if(position % 2 == 0){
   backgroundResource = getResources.getColor(android.R.color.WHITE);
} else{
   backgorundResource = getResources.getColor(android.R.color.RED);
}
view.setBackground(backgroundResource);

当然,以上只是伪代码,可能需要根据你的项目进行调整。


上述解决方案仅适用于现有数据。如果无论是否有数据都需要交替颜色,如果我现在理解这就是您在 dispatchDraw 中试图实现的目标。老实说,我不是 100% 确定如何执行此操作,也无法对其进行测试,但我想这些步骤是这样的:

  • 获取 ListView 的高度
  • 获取 ListView 的宽度
  • 获取一个 child 的高度(listPreferredItemHeight,如果您使用它。如果您使用包装内容,这可能会比较棘手,因为您无法预测项目的大小,因此为空的 ListView 交替颜色会很困难)。
  • 当 ListView 中还有空间时,绘制一个矩形。

请注意,您不能根据 child 的数量进行迭代,因为此时您可能没有任何 child 。

伪代码:

listViewWidth = getMeasuredWidth();
listViewHeight = getMeasuredHeight();
numChildren = getChildCount();
itemHeight = getItemHeight(); // See comments above, adjust this for your problem.
currentTop = 0; // Used to keep track of the top of the rectangle we are drawing.
currentBottom = itemHeight; // Used to keep track of the bottom rectangle we are currently drawing.

int currentRectangle = 0;
while(currentBottom <= listViewHeight){
   if(currentRectangle  % 2 == 0){
      mPaintBackground.setColor(Color.WHITE);
   } else{
      mPaintBackground.setColor(Color.RED);
   }

   Rect r = new Rect(0, currentBottom, getMeasuredWidth(), getMeasuredHeight());
   canvas.drawRect(r, mPaintBackground);

   // Move to next
   currentTop += itemHeight;
   currentBottom += itemHeight;
   currentRectangle++;
}

关于android - 即使没有数据,ListView 类的备用背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31918038/

相关文章:

java - 添加到listView的顶部

c# - 如何在 ListView 绑定(bind) Xamarin.Forms 中创建网格

javascript - ListView 与 NumericTextBox 数据初始化

android - ListView 游标适配器,总是在点击时获得第一项

android - 按键不起作用

android - 如何将 SQLite DBstorm-gen 与 android studio 一起使用并生成注释?

java - 安卓 : Setting and getting clicked item id for a list containing text and images.

c# - 在 ListView 中绑定(bind)图像只显示字符串

android glsurfaceview空指针异常

java - 使用内容提供程序获取日历实例