【问题标题】:Android Listview changing colour of certian positionsAndroid Listview改变某些位置的颜色
【发布时间】:2017-02-16 12:34:24
【问题描述】:

我有一个适配器从检索到的 json 填充我的 Listview。 我想根据位置更改某些单元格的颜色,即位置 1 蓝色位置 2 红色等。但是使用当前代码我得到了想要的效果,但是当我向下滚动列表时它会重复。我知道这是因为视图令人耳目一新,但不知道如何修复它。

public class ListAdapter extends BaseAdapter {

MainActivity main;

ListAdapter(MainActivity main) {
this.main = main;
}

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

@Override
public Object getItem(int position) {
return null;
}

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

static class ViewHolderItem {
TextView name;
TextView code;
TextView pts;

}

@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolderItem holder = new ViewHolderItem();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) main.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       convertView = inflater.inflate(R.layout.cell, null);

       holder.name = (TextView) convertView.findViewById(R.id.name);
       holder.code = (TextView) convertView.findViewById(R.id.code);
       holder.pts = (TextView) convertView.findViewById(R.id.pts);

       convertView.setTag(holder);
}
else  { 
          holder = (ViewHolderItem) convertView.getTag();
}

holder.name.setText(this.main.countries.get(position).name);
holder.code.setText(this.main.countries.get(position).code);
holder.pts.setText(this.main.countries.get(position).pts);

 if (position == 1) { 
holder.name.setBackgroundColor(Color.parseColor("#FFFFFF"));

}

return convertView;

}

}

【问题讨论】:

  • 为 textview 背景提供所需颜色的 else 条件
  • 谢谢,我试过了,但它仍然在整个列表中重复。
  • 您可以创建一个列表大小的颜色列表,并按位置从 colorList 中获取您想要的颜色

标签: java android listview colors


【解决方案1】:

列表中的视图被回收。那就是滚动到熨平板外面的项目视图在滚动到屏幕的项目中被重用。使用这样的代码。

if (position == 1) {
  holder.name.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else {
  holder.name.setBackgroundColor(...)   
}

或者如果你想只用两个项目颜色来划分列表:

if(position % 2 == 0) view.setBackgroundColor(Color.rgb(224, 224, 235));
if(position % 2 == 1) view.setBackgroundColor(The normal color you should set);

【讨论】:

    【解决方案2】:

    如果我理解正确 - 您会反复看到这种颜色,而不仅仅是在位置 1。 可能会发生这种情况,因为您正在重新使用 getView 中提供的 convertView,这意味着您获得了一个已经创建并已使用的 View,它具有背景颜色并具有较旧的属性,并且您为其提供了新的属性和数据。 因此,在这种情况下 - 它可能具有旧颜色,并且因为您没有为 - 每个位置指定颜色,所以该颜色会一直存在。

    我认为在这种情况下 -

    switch(position){
         case 1:
              holder.name.setBackgroundColor(Color.parseColor("#FFFFFF"));
              break;
         case 2:
              holder.name.setBackgroundColor(Color.parseColor("#EEEEEE"));
              break;
         case 3:
              holder.name.setBackgroundColor(Color.parseColor("#000000"));
              break;
         case 4:
              holder.name.setBackgroundColor(Color.parseColor("#DDDDDD"));
              break;
         case default:
              holder.name.setBackgroundColor(Color.parseColor("#ABCDEF"));
              break;
    }
    

    将解决回收色问题。 (在开关选择范围内添加任意数量的颜色,或者创建一个如上所述的颜色数组并根据位置进行更改。

    【讨论】:

      猜你喜欢
      • 2012-05-01
      • 2021-01-02
      • 2011-08-20
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多