ListView类的备用背景颜色,即使没有数据也是如此

Piy*_*ush 16 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;
    }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

适配器内部:

    if (position % 2 == 0) {
        view.setBackgroundColor(Color.RED);
    } else {
        view.setBackgroundColor(Color.WHITE);
    }
Run Code Online (Sandbox Code Playgroud)

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

Ada*_*331 13

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

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

如果它有帮助,请将您的计数器变量重命名为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);
Run Code Online (Sandbox Code Playgroud)

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


上述解决方案仅适用于现有数据.如果你需要一种交替的颜色,无论是否有数据,如果我现在理解的是你想要实现的目标dispatchDraw.我会非常诚实地告诉我,我不是100%确定如何做到这一点,我无法测试它,但我想这些步骤是这样的:

  • 获取ListView的高度
  • 获取ListView的宽度
  • 找一个孩子的身高(listPreferredItemHeight,如果你使用它.如果你使用包的内容,因为你无法预测项目的大小,如此交替颜色空的ListView将是困难的,这可能是麻烦).
  • 虽然ListView中还有空间,但绘制一个矩形.

请注意,您不能根据子项的数量进行迭代,因为此时您可能没有.

伪代码:

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++;
}
Run Code Online (Sandbox Code Playgroud)


Piy*_*ush 11

最后,我在@ McAdam331的巨大帮助下得到了答案.使用他的代码后,我得到了一些奇怪的东西,但在那之后我用这个修复了代码

        int listViewHeight = getMeasuredHeight();
        int itemHeight = lastChild.getMeasuredHeight();
        int currentTop = 0;
        int currentBottom = lastChild.getBottom();

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

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

            // Move to next
            currentTop += itemHeight;
            currentBottom += itemHeight;
            currentRectangle++;
        }
Run Code Online (Sandbox Code Playgroud)