备用背景颜色的ListView类即使没有数据 [英] Alternate background color for ListView class even without data

查看:104
本文介绍了备用背景颜色的ListView类即使没有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to set an alternating color for my custom ListView class.

在code为如下:

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 ,我用这个code:

To get an alternating background color for ListView, I have used this code:

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

但它始终显示一种颜色,红色或白色的一切我试试。我没有得到交​​替颜色白 - 红 - 白 - 红。

But it always shows one color, red or white with everything I try. I am not getting alternating colors white-red-white-red.

推荐答案

这是失败的原因是因为你的for循环永远不会改变。你总是检查 getChildCount()%2 getChildCount()将返回相同的每个迭代。你需要根据位置做你的检查:

The reason this is failing is because your for loop never changes. You are always checking getChildCount() % 2. getChildCount() will return the same for each iteration. You need to do your check based on position:

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

如果有帮助,从重命名计数器变量i 位置所以,这将是更具可读性你在未来,或者,可以记它来帮助自己的。

If it helps, rename your counter variable from i to position so that this will be more readable for you in the future, or make a note of it to help yourself out.

我也想补充一点,考虑到code你有现在,你的循环是不会改变任何事情。它仅仅是通过迭代孩子的数量和设置 mPaintBackground 。在结束时,将留下的任何值时,它接收从最后一次迭代

I would also like to add that, given the code you have now, your for loop isn't changing anything. It is just iterating through the number of children and setting mPaintBackground. In the end, it will be left with whatever value it receives from the last iteration.

我觉得处理绘制背景颜色将是对于ListView适配器的最好方法,在这种情况下,你可以重写<一个href="http://developer.android.com/reference/android/widget/Adapter.html#getView(int,%20android.view.View,%20android.view.ViewGroup)">getView()并以此为基础进行的位置检查参数:

I think the best way to handle drawing the background color would be in the adapter for the Listview, in which case you can override getView() and do a check based on the position parameter:

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

当然,以上只是伪code,则可能需要调整您的项目。

Of course, the above is just pseudocode, it may need to be adjusted to your project.

上面的解决方案将只对现有的数据的工作。如果你不管是否有数据,这要是我理解需要一个交替的颜色,现在是你试图达到 dispatchDraw 。我会很诚实,我不是100%确定如何做到这一点,我不能测试它,但我想会是这样的步骤:

The above solution will work only for existing data. If you need an alternating color regardless of whether or not there is data, which if I understand now is what you were trying to achieve in dispatchDraw. I will be very honest that I am not 100% sure how to do this, and I cannot test it, but I imagine the steps going like this:

  • 获取的ListView的高度
  • 获取的ListView的宽度
  • 在获得一个孩子(名单preferredItemHeight的高度,如果你使用它。如果你使用包的内容,这可能是棘手的,因为你不能pdict的项目的大小$ P $,如此交替颜色空ListView的将是困难的)。
  • 虽然有剩余空间在ListView,绘制一个矩形。

请注意在这里,您不可以根据子女人数迭代,因为你可能没有任何在这一点上。

Note here that you cannot iterate based on number of children, because you might not have any at this point.

伪code:

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

这篇关于备用背景颜色的ListView类即使没有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆