简体   繁体   English

Android ListView微粒行背景颜色变化

[英]Android ListView particulate row background color change

I have a custom ListView where I need to show Available shift in Green Color and Unavailable shift in Red. 我有一个自定义ListView ,我需要显示绿色可移Color和红色不可移。 I made a custom adapter in it's getView() I am trying to check the value and change the color. 我在getView()制作了一个自定义适配器,试图检查该值并更改颜色。

GetView in Custom Adapter looks like this- 自定义适配器中的GetView看起来像这样-

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(v==null){
        v = inflater.inflate(R.layout.simplerow, null);
    }

    ViewHolder holder = null;
    if(convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.simplerow, null);
        holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
        holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


        if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
            ((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
        }


    holder.textView1.setText(objects.get(position).getprop1());
    holder.textView2.setText(objects.get(position).getprop2());
    return convertView;
}

But the out put I am getting like this - 但是我得到的结果是-

在此处输入图片说明

And whenever I am scrolling color are showing in some other value too. 每当我滚动时,颜色也会显示其他值。

Replace you getView() by 将您的getView()替换为

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;
    if(convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.simplerow, null);
        holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
        holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

 /**
  * here check your condtion like this and set color to background no need of for loop
  ***/
        if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
            ((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
        }else
       {
              // set normal color here
       }


    holder.textView1.setText(objects.get(position).getprop1());
    holder.textView2.setText(objects.get(position).getprop2());
    return convertView;
}

While you are setting red background for one View you have to set normal background for remaining views by using else case 在为一个视图设置红色背景时,必须使用else case设置其余视图的普通背景

if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
        ((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
 } else{
   //here set normal background for view.
 }

Hope this will helps you. 希望这对您有帮助。

It is due to View Reuse capability of ListView. 这是由于ListView具有视图重用功能。 If Shift -3 condition is not met you have to set background of the row to default row color since the row with red background can be reused at any position in list. 如果不满足Shift -3条件,则必须将行的背景设置为默认行颜色,因为具有红色背景的行可以在列表中的任何位置重复使用。

if (objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
            ((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow))
                    .setBackgroundResource(R.drawable.red_bg);
        } else {
            //default bg
            ((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow))
                    .setBackgroundResource(deafult_bg);
        }

Here is the solution. 这是解决方案。

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.simplerow, null);
            holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
            holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        if (objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
            ((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
        } else {
            //Update to default color
        }

        holder.textView1.setText(objects.get(position).getprop1());
        holder.textView2.setText(objects.get(position).getprop2());
        return convertView;
    }

If the condition is matched set the RED color else set the default color. 如果条件匹配,则设置RED否则设置默认颜色。

You also need to add else part here as below - 您还需要在此处添加其他部分,如下所示:

for (int i = 1; i < 9 ; i++) {
    if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
        ((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
    }
    else{
        ((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setB ackgroundResource(R.drawable.green_bg); //your green color
    }
}

You have to use getViewType() . 您必须使用getViewType() Try this code (I modified your code for you): 尝试以下代码(我为您修改了代码):

@Override
public int getItemViewType(int position) {
    return (objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) ? 1 : 0;
}

@Override
public int getViewTypeCount() {
    return 2;
}        

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;
    int type = getItemViewType(position);
    if(convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.simplerow, null);
        holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
        holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if(type == 0) {

            //Set here your regular color
    }else {
        ((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
    }

    holder.textView1.setText(objects.get(position).getprop1());
    holder.textView2.setText(objects.get(position).getprop2());
    return convertView;
}

EDIT: I have to explain. 编辑:我必须解释。 If you don't use getViewType() method and just if statement like other answers here, then if you have many items in your list (enough for scrolling), your items will change the color dynamically while scrolling. 如果您不使用getViewType()方法,并且就像此处的其他答案一样,则如果列表中有很多项(足以滚动),则项将在滚动时动态更改颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM