动态地改变TextView的字体颜色的ListView字体、颜色、动态、ListView

2023-09-05 07:42:40 作者:酷的没边

我绑定的XML文档,以自定义适配器。所有的在列表中的项目最初具有的白色字体颜色。一个XML文档中的节点都有,我检查,如果该属性设置,我想改变该项目的字体颜色在ListView到较深的颜色属性。在code我似乎工作开始,但如果我滚动列表向上和向下,在ListView中的项目应保持白色,以深色自动改变,由于某些原因:

下面是我的布局code:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
 机器人:方向=垂直>
 < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /文
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:TEXTSIZE =15dip
    机器人:文字颜色=#FFFFFF
    机器人:TEXTSTYLE =黑体
    机器人:填充=5像素
     />
 < / LinearLayout中>
 

和这里是我的自定义适配器:

 私有类CustomAdapter扩展了BaseAdapter
 {
    私人的ArrayList<的FilterItem> MDATA =新的ArrayList<的FilterItem>();
    私人LayoutInflater mInflater;

    公共CustomAdapter(){
        mInflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    公共无效的addItem(的FilterItem项目){
        mData.add(项目);
    }

    @覆盖
    公众诠释getCount将(){
        返回mData.size();
    }

    @覆盖
    公共对象的getItem(INT位置){
        返回mData.get(位置);
    }

    @覆盖
    众长getItemId(INT位置){
        返回的位置;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {
        ViewHolder持有人;
        的FilterItem项目=(的FilterItem)this.getItem(位置);

        如果(convertView == NULL)
        {

            持有人=新ViewHolder();

            convertView = mInflater.inflate(R.layout.main,NULL);
            holder.text =(TextView中)convertView.findViewById(R.id.text);

            convertView.setTag(保持器);

        } 其他 {
            支架=(ViewHolder)convertView.getTag();
        }

            TextView的tvText = holder.text;

            tvText.setText(item.getTitle());

            如果(item.Read())
            {
                tv.setTextColor(Color.Gray);
            }

        返程(convertView);
    }
}
 
如何将PPT里的文字全部改变字体颜色,

解决方案

ListView控件重用视图,因为你可以在 getView 看你convertView,你只能从改变颜色白色至灰色。你永远不回恢复颜色为白色。我建议

 如果(item.Read()){
    tv.setTextColor(Color.Gray);
} 其他 {
    tv.setTextColor(Color.White);
}
 

I am binding an XML document to a custom adapter. All of the items in the list initially have a font color of white. One of the nodes in the XML document has an attribute that I am checking and if the attribute is set, I'd like to change the font color of that item in the ListView to a darker color. The code I have seems to work initially, but if I scroll the list up and down, the items in the ListView that should stay white, change automatically to the dark color, for some reason:

Here is my layout code:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
 android:orientation="vertical">
 <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:textSize="15dip" 
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:padding="5px"
     />
 </LinearLayout>

and here is my custom adapter:

 private class CustomAdapter extends BaseAdapter 
 {       
    private ArrayList<FilterItem> mData = new ArrayList<FilterItem>();
    private LayoutInflater mInflater;

    public CustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(FilterItem item) {
        mData.add(item);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        FilterItem item = (FilterItem)this.getItem(position);

        if (convertView == null)
        {

            holder = new ViewHolder();

            convertView = mInflater.inflate(R.layout.main, null); 
            holder.text = (TextView)convertView.findViewById(R.id.text);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder)convertView.getTag();
        }

            TextView tvText = holder.text;

            tvText.setText(item.getTitle());

            if (item.Read())
            {
                tv.setTextColor(Color.Gray);
            }

        return(convertView);
    }
}

解决方案

ListView reuses view, as you can see in getView you get convertView, and you only change color from white to gray. You never restore color back to white. I suggest

if (item.Read()) {
    tv.setTextColor(Color.Gray);
} else {
    tv.setTextColor(Color.White);
}