特定日期过后更改视图
Change view after certain date has passed
Soo,我有一个应用程序,基本上可以列出足球比赛的不同日期。就像一个时间表之类的东西。
游戏在不同的片段中使用适配器和 RecyclerView 列出。我的问题是这样的。当我用视图加载片段时,如何根据已通过的日期简单地更改列表中的 TextView 或背景?例如:
一行列出了 2016 年 4 月 3 日玩过的一款游戏。在这个日期过去之后,我想更改该行的背景以向用户显示该游戏不可用,可能背景较暗或类似的东西。
我进行了一些搜索,但找不到任何相似的内容。我在想 "if, else" 语句之类的东西?我怎样才能完成这样的事情?
您希望在自定义适配器 class 中执行此操作,在其中填充每个列表项中的信息。它基本上看起来像这样:
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
}
}
Soo,我有一个应用程序,基本上可以列出足球比赛的不同日期。就像一个时间表之类的东西。
游戏在不同的片段中使用适配器和 RecyclerView 列出。我的问题是这样的。当我用视图加载片段时,如何根据已通过的日期简单地更改列表中的 TextView 或背景?例如:
一行列出了 2016 年 4 月 3 日玩过的一款游戏。在这个日期过去之后,我想更改该行的背景以向用户显示该游戏不可用,可能背景较暗或类似的东西。
我进行了一些搜索,但找不到任何相似的内容。我在想 "if, else" 语句之类的东西?我怎样才能完成这样的事情?
您希望在自定义适配器 class 中执行此操作,在其中填充每个列表项中的信息。它基本上看起来像这样:
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
}
}