从4种颜色的组中设置列表项中的颜色

时间:2016-03-22 08:42:18

标签: android listview android-adapter

在我的应用中,我有一个列表视图。在列表项的每一行中都有一个textview,它显示了名称的初始字符,就像许多联系人应用程序一样。我必须从colors.xml中定义的4种颜色的预定义颜色设置该textview的背景颜色。我想要的是在listview中每4个项目之后颜色应该重复。为此,我使用了以下逻辑

 color_array=activity.getResources().getIntArray(R.array.dar_list_color);

    if (count<4) {
   viewHolder.layout_color.setBackgroundColor(color_array[count]);

  }

  else
  count=0;


  count++;

这个工作正常,但有一个问题,列表视图的常见问题,即滚动颜色更改。由于计数与适配器的位置无关,我无法弄清楚如何解决这个问题。

以下是我实施的屏幕截图 Current Result

这是完整的适配器代码

public class DAR_Aadapter extends BaseAdapter  {
 int color_array[],count=0;
 Activity activity;
 ArrayList<DAR_List_Item> dar_List_Items;

 public DAR_Aadapter(Activity activity, ArrayList<DAR_List_Item> dar_List_Items) {
  // TODO Auto-generated constructor stub

  this.activity=activity;
  this.dar_List_Items=dar_List_Items;
  color_array=activity.getResources().getIntArray(R.array.dar_list_color);

 }


 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return dar_List_Items.size();
 }

 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return dar_List_Items.get(position);
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return 0;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  ViewHolder viewHolder;
  if(convertView==null)
  {
   viewHolder = new ViewHolder();
   convertView = LayoutInflater.from(activity).inflate(R.layout.dar_swipe_list_layout, null);

   viewHolder.clientname= (TextView)convertView.findViewById(R.id.Client_name);
   viewHolder.client_initial= (TextView) convertView.findViewById(R.id.client_initial);
   viewHolder.text_contactedpaerson= (TextView)convertView.findViewById(R.id.text_contacted);
   viewHolder.text_contacttype= (TextView)convertView.findViewById(R.id.text_contact_type);
   viewHolder.text_createddate= (TextView)convertView.findViewById(R.id.text_Created_date);
   viewHolder.text_requestdate= (TextView)convertView.findViewById(R.id.text_request_date);
   viewHolder.text_productname= (TextView)convertView.findViewById(R.id.text_product);
//   viewHolder.text_status= (TextView)convertView.findViewById(R.id.text_status);


   viewHolder.contactedpaerson= (TextView)convertView.findViewById(R.id.contacted);
   viewHolder.contacttype= (TextView)convertView.findViewById(R.id.contact_type);
   viewHolder.createddate= (TextView)convertView.findViewById(R.id.create_date);
   viewHolder.requestdate= (TextView)convertView.findViewById(R.id.request_date);
   viewHolder.productname= (TextView)convertView.findViewById(R.id.product);
//   viewHolder.status= (TextView)convertView.findViewById(R.id.status);
   viewHolder.layout_color= (LinearLayout) convertView.findViewById(R.id.client_initial_layout);

   convertView.setTag(viewHolder);

  }else
  {
   viewHolder = (ViewHolder)convertView.getTag();
  }
   viewHolder.clientname.setText(dar_List_Items.get(position).getClient_name());
      String temp=viewHolder.clientname.getText().toString();
      viewHolder.client_initial.setText(""+temp.charAt(0));
   viewHolder.contactedpaerson.setText(dar_List_Items.get(position).getContacted_person());
   viewHolder.contacttype.setText(dar_List_Items.get(position).getContact_type());
   viewHolder.createddate.setText(dar_List_Items.get(position).getCreated_date());
   viewHolder.productname.setText(dar_List_Items.get(position).getproduct_name());
   viewHolder.requestdate.setText(dar_List_Items.get(position).getRequest_date());
//   viewHolder.status.setText(dar_List_Items.get(position).getStatus());

  if (count<4) {
   viewHolder.layout_color.setBackgroundColor(color_array[count]);

  }

  else
  count=0;


  count++;



   return convertView;


 }




 public static class ViewHolder
 {
  LinearLayout layout_color;
  TextView clientname,client_initial,text_contactedpaerson,contactedpaerson,text_contacttype,contacttype,text_productname,productname,text_status,status,createddate,text_createddate,text_requestdate,requestdate;
 }

}

1 个答案:

答案 0 :(得分:2)

这样的事情怎么样?

count = position % color_array.length();
switch(count) 
{
      case 0:
      viewHolder.layout_color.setBackgroundColor(color_array[count]);
      break;

      case 1:
      case 2:
      case 3:
        //handle these cases separately
        //viewHolder.layout_color.setBackgroundColor(color_array[count]); 
        break;

}

它使您可以灵活地分别处理每个案例。您在哪里初始化count