Change view after certain date has passed

时间:2016-04-04 16:37:51

标签: android android-layout date android-recyclerview adapter

Soo, I've got an app which basically lists different dates when a football game is played. Like an schedule sort of thing.

The games are listed by using an adapter and RecyclerView in different fragments. My question is this. How can I simply change the TextView or background in the list based on the dates that has been passed when i load up the fragment with the views? For example:

A row lists one game that was played April 3 2016. After this date has passed i would like to change the background on the row to show the user that this game isn't available, perhaps with a darker background or something like that.

I did some searching but couldn't find anything that were similar. I'm thinking something like "if, else" statement perhaps? How could I accomplish something like this?

1 个答案:

答案 0 :(得分:1)

You'd want to do this in your custom Adapter class where you populate the info in each list item. It would basically look something like this:

private Arraylist<String> dates; // Pass this value when you construct Adapter class

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    Calendar now = Calendar.getInstance();

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); // date strings should follow this specified format - e.g. 04/04/2016 15:05:00
    Calendar gameTime = Calendar.getInstance();
    gameTime.setTime(sdf.parse(dates.get(position)));
    int after = gameTime.compareTo(now);

    if (after < 0) {
        // Game time has passed, change background color
    } else {
        // Game is still in the future
    }
}