Android背景颜色无法正确显示

时间:2015-08-12 23:44:36

标签: android background

第一次在这里,对Android很新。在我的"活动页面"。

中,我的背景颜色有问题

当显示新时间时,时间的背景应该在深棕色和浅棕色之间交替,但由于某种原因,它有时会起作用,并且在其他时间不起作用。

当应用程序显示不正确时,它会在时间变化时保持相同的棕色阴影,或者当它保持不变时会改变棕色的阴影。我不确定是什么导致它。

数据来自托管在服务器上的json文件。数据加载到AsyncTask类中,应用程序将等待数据在用户到达事件页面之前下载。将显示一条消息,说明"请等待下载数据"如果用户点击事件。

以下是其中一个条目的示例:

{
  "title": "Some_Title",
  "description": "Some_Description\n",
  "times": [
    {
      "startTime": "2014-09-20 09:30:00",
      "endTime": "2014-09-20 10:30:00"
    },
    {
      "startTime": "2014-09-20 11:00:00",
      "endTime": "2014-09-20 12:00:00"
    },
    {
      "startTime": "2014-09-20 13:00:00",
      "endTime": "2014-09-20 14:00:00"
    }
  ],
  "locationTitle": "Some_Location",
  "locationLat": ##.######,
  "locationLng": -###.######,
  "typeX": 0,
  "typeY": true,
  "typeZ": false,
  "typeA": false,
  "typeB": false,
  "typeC": false,
  "SpreadsheetName": "Some_Name\n(A_Location)"
},

颜色在Adapter类中处理。以下是处理背景颜色的代码以及事件页面的屏幕截图以及Log:

public class EventScheduleAdapter extends BaseAdapter {

private final ArrayList<Event> events;
private final Context context;
private final ArrayList<Integer> holders;

public EventScheduleAdapter(Context context, boolean[] types, Calendar day) {
    this.context = context;
    this.events = EventList.getInstance().getFilteredEvents(day, types);
    holders = new ArrayList<Integer>();
}

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

@Override
public Object getItem(int i) {
    return events.get(i);
}

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

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
    Event event = events.get(position);
    View vi = convertView;
    ViewHolder holder;
    //ImageView image = new ImageView(this.context);
    //FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
    //params.setMargins(20, 20, 20, 20);

    if (convertView == null) {
        vi = View.inflate(context, R.layout.event_list_item, null);
        holder = new ViewHolder();
        holder.time = (TextView) vi.findViewById(R.id.timeNumber);
        holder.am = (TextView) vi.findViewById(R.id.timeAM);
        holder.title = (TextView) vi.findViewById(R.id.title);
        holder.location = (TextView) vi.findViewById(R.id.location);
        //holder.description = (TextView) vi.findViewById(R.id.description);
        holder.color = (LinearLayout) vi.findViewById(R.id.color);
        holder.timeBackground = (RelativeLayout) vi.findViewById(R.id.time);
        //holder.image = (ImageView) vi.findViewById(R.id.tag_A);
        vi.setTag(holder);
    } else {
        holder = (ViewHolder) vi.getTag();
    }

    if(event.getStartTime() != null && (position == 0 || !EventList.isSameTime(event.getStartTime(), events.get(position - 1).getStartTime()))) {
        holder.time.setText(String.format("%tl:%tM", event.getStartTime(), event.getStartTime()));
        holder.am.setText(String.format("%Tp", event.getStartTime()));
    } else {
        holder.time.setText("");
        holder.am.setText("");
    }
    Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/main.ttf");
    holder.title.setText(event.getTitle());
    holder.title.setTypeface(font);
    holder.location.setText(event.getLocationTitle());
    holder.location.setTypeface(font);
    //holder.description.setText(event.getDescription());
    //holder.description.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/description.ttf"));
    holder.color.removeAllViews();
    if(event.isTypeA()) {
        holder.color.addView(newColorView(R.drawable.tag_A));
    } if(event.isTypeB()) {
        holder.color.addView(newColorView(R.drawable.tag_B));
    } if(event.isTypeC()) {
        holder.color.addView(newColorView(R.drawable.tag_C));
    } if(event.isTypeD()) {
        holder.color.addView(newColorView(R.drawable.tag_D));
    } if(event.isTypeE()) {
        holder.color.addView(newColorView(R.drawable.tag_E));
    }

    if (position != 0) {
        Log.i("EventScheduleAdapter", "position " + position + "event @ position: " + event.getTitle());
        if (!EventList.isSameTime(event.getStartTime(), events.get(position - 1).getStartTime())) {
            Log.i("EventScheduleAdapter", "Same time is FALSE");
            if(holders.get(position - 1) == R.color.background_event_dark) {
                Log.i("EventScheduleAdapter", "Different Color LIGHT");
                holder.timeColor = R.color.background_event_light;
                holders.add(holder.timeColor);
                Log.i("EventScheduleAdapter", "position Color" + holders.get(position));
            } else {
                Log.i("EventScheduleAdapter", "Different Color DARK");
                holder.timeColor = R.color.background_event_dark;
                holders.add(holder.timeColor);
                Log.i("EventScheduleAdapter", "position Color" + holders.get(position));
            }
        } else {
            Log.i("EventScheduleAdapter", "Same time is TRUE");
            holder.timeColor = holders.get(position - 1);
           // holder.
            //holder.timeColor = holder.get(position-1)
            if (holder.timeColor == R.color.background_event_dark)
                Log.i("EventScheduleAdapter", "SAME COLOR IS DARK");
            if (holder.timeColor == R.color.background_event_light)
                Log.i("EventScheduleAdapter", "SAME COLOR IS LIGHT");
            holders.add(holder.timeColor);
            Log.i("EventScheduleAdapter", "position Color" + holders.get(position));
        }
    } else {
        Log.i("EventScheduleAdapter", "Position is 0");
        holder.timeColor = R.color.background_event_dark;
        holders.add(holder.timeColor);
    }

    holder.timeBackground.setBackgroundColor(context.getResources().getColor(holder.timeColor));
    return vi;
}

 /*   Circle newColorView(int color) {
    Circle layout = new Circle(context);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
    params.setMargins(5, 5, 5, 5);
    layout.setLayoutParams(params);
    layout.setColor(color);
    return layout;
} */

ImageView newColorView(int drawable) {
    ImageView layout = new ImageView(context); // put drawable in here
    LinearLayout.LayoutParams params;
    if (drawable == R.drawable.tag_cp) {
        params = new LinearLayout.LayoutParams(78, 50);
    }
    else if (drawable == R.drawable.tag_slo) {
        params = new LinearLayout.LayoutParams(100, 50);
    }
    else {
        params = new LinearLayout.LayoutParams(50, 50);
    }
    params.setMargins(5, 5, 5, 5);
    layout.setLayoutParams(params);
    layout.setImageResource(drawable);
    return layout;
}

class ViewHolder {
    int timeColor;
    RelativeLayout timeBackground;
    TextView time;
    TextView am;
    TextView title;
    TextView location;
    //TextView description;
    LinearLayout color;
}
}

由于我是新成员,我无法发布图片:(但我会尽快发布

1 个答案:

答案 0 :(得分:1)

您继续使用您创建的每个项目添加到holders数组中。因此,如果要求您绘制item0 3次并将item1绘制3次,则最终会在阵列中绘制6个条目。考虑使用sparse arrayint[]。 例如,当您被要求为位置X创建视图时,您可以:

holders[X] = myColorId;