列表视图行获得混合色彩加载和滚动时视图、加载、色彩、列表

2023-09-06 16:31:22 作者:你是我预订的人

我设置基于它的儿童中的一个的文本值的行的背景色。然而,多个背景正在考虑其文本值的设置。向上和向下滚动时,事情变得更糟糕。在code适配器:

包com.test.app;进口的java.util.ArrayList;进口android.content.Context;进口android.util.Log;进口android.view.LayoutInflater;进口android.view.View;进口android.view.ViewGroup;进口android.widget.BaseAdapter;进口android.widget.ImageView;进口android.widget.LinearLayout;进口android.widget.TextView;公共类MyBaseAdapter延伸BaseAdapter {    私人LayoutInflater mInflater = NULL;    私人的ArrayList<的String []> mItems =新的ArrayList<的String []>();    公共MyBaseAdapter(上下文的背景下,ArrayList的<的String []>项目){         mItems =物品;         mInflater = LayoutInflater.from(上下文);    }    公共无效的addItem(字符串[]吧){        mItems.add(它);    }    公共无效setListItems(ArrayList的<的String []> LIT){        mItems =亮;    }    @覆盖    公众诠释的getCount(){        返回mItems.size();    }    @覆盖    公共对象的getItem(INT位置){        返回mItems.get(位置);    }    @覆盖    众长getItemId(INT位置){        返回的位置;    }    静态类ViewHolder {        公众的TextView TV0,TV1;    }    @覆盖    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){        查看rowView = NULL;        ViewHolder viewHolder;        如果(convertView == NULL)        {            rowView = mInflater.inflate(R.layout.history_row,NULL);        }        其他        {            rowView = convertView;        }        viewHolder =新ViewHolder();        viewHolder.tv0 =(TextView中)rowView.findViewById(R.id.textView0);        viewHolder.tv1 =(TextView中)rowView.findViewById(R.id.textView1);        rowView.setTag(viewHolder);        ViewHolder支架=(ViewHolder)rowView.getTag();        holder.tv0.setText(mItems.get(位置)[0]的ToString());        holder.tv1.setText(mItems.get(位置)[1]的ToString());        如果(holder.tv1.getText()。等于(0))        {            rowView.setBackgroundColor(0xAA777777);            //只含0的行应该是彩色的,但它的颜色多,随机行。        }        返回rowView;    }}

解决方案

如果你只是用:

 如果(holder.tv1.getText()。等于(0)){            rowView.setBackgroundColor(0xAA777777);            //只含0的行应该是彩色的,但它的颜色多,随机行。} 
XTools 升级历史

这确实使含 0 该行有特定的颜色,但是你会滚动列表上下,与此颜色特定的行会得到回收和在地方结束了它不应该。正确的方法是提供一个默认颜色的行,如果它不包含 0 所以你重写一个可能的回收视图的色泽不好:

 如果(holder.tv1.getText()。等于(0)){            rowView.setBackgroundColor(0xAA777777);            //只含0的行应该是彩色的,但它的颜色多,随机行。}其他{   rowView.setBackgroundColor(/ *这里默认的颜色将* /)   //这行不包含0,所以它必须有默认的颜色。  //因为你可以用回收的视图处理(即具有上述色) //那么我们必须恢复的颜色为默认,以确保我们最终有正确的颜色。} 

另外,正确的code为 ViewHolder 模式是:

 查看rowView = convertView;    ViewHolder viewHolder;    如果(rowView == NULL){        rowView = mInflater.inflate(R.layout.history_row,父母,假);        viewHolder =新ViewHolder();        viewHolder.tv0 =(TextView中)rowView.findViewById(R.id.textView0);        viewHolder.tv1 =(TextView中)rowView.findViewById(R.id.textView1);        rowView.setTag(viewHolder);    }其他{        viewHolder =(ViewHolder)rowView.getTag();    } 

然后使用 viewHolder 对象来设置行的数据。

I'm setting the background color of a row based on a text value of one of it's children. However, multiple backgrounds are being set regardless of its text value. It gets worse when scrolling up and down. The code for the adapter:

package com.test.app;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyBaseAdapter extends BaseAdapter {

    private LayoutInflater mInflater = null;
    private ArrayList<String[]> mItems = new ArrayList<String[]>();

    public MyBaseAdapter(Context context, ArrayList<String[]> items) {
         mItems = items;
         mInflater = LayoutInflater.from(context);
    }

    public void addItem(String[] it) {
        mItems.add(it);
    }

    public void setListItems(ArrayList<String[]> lit) {
        mItems = lit;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder {
        public TextView tv0,tv1;
    }

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

        View rowView = null;
        ViewHolder viewHolder;

        if(convertView == null)
        {
            rowView = mInflater.inflate(R.layout.history_row, null);
        }
        else
        {
            rowView = convertView;
        }

        viewHolder = new ViewHolder();

        viewHolder.tv0 = (TextView)rowView.findViewById(R.id.textView0);
        viewHolder.tv1 = (TextView)rowView.findViewById(R.id.textView1);

        rowView.setTag(viewHolder);

        ViewHolder holder = (ViewHolder) rowView.getTag();

        holder.tv0.setText(mItems.get(position)[0].toString());
        holder.tv1.setText(mItems.get(position)[1].toString());

        if(holder.tv1.getText().equals("0"))
        {
            rowView.setBackgroundColor(0xAA777777);
            // Only the row containing "0" should be colored, yet it colors multiple, random rows.
        }

        return rowView;
    }

}

解决方案

If you just use:

if(holder.tv1.getText().equals("0")) {
            rowView.setBackgroundColor(0xAA777777);
            // Only the row containing "0" should be colored, yet it colors multiple, random  rows.
}

this will indeed make the row containing 0 to have that particular color but as you will scroll the list up and down, this particular row with this color will get recycled and end up in places where it shouldn't be. The correct way is to provide a default color to the row if it doesn't contain 0 so you override the bad color of a possible recycled view:

if(holder.tv1.getText().equals("0")) {
            rowView.setBackgroundColor(0xAA777777);
            // Only the row containing "0" should be colored, yet it colors multiple, random  rows.
} else {
   rowView.setBackgroundColor(/*Here the default color will be*/)
   // This row doesn't contain 0 so it must have the default color.
  // Because you could be dealing with a recycled view(that has the above color)
 // then we must revert the color to the default to be sure we end up with the correct color.
}

Also the correct code for the ViewHolder pattern is:

    View rowView = convertView;
    ViewHolder viewHolder;

    if(rowView == null) {
        rowView = mInflater.inflate(R.layout.history_row, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.tv0 = (TextView)rowView.findViewById(R.id.textView0);
        viewHolder.tv1 = (TextView)rowView.findViewById(R.id.textView1);
        rowView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) rowView.getTag();    
    }             

and then you use the viewHolder object to setup the row's data.