这个问题不是很清楚。我在问题的评论中提到了两个问题。
- 是否要突出显示某些特定行?
- 您想每两秒切换一次突出显示吗?
所以我要为这两者寻找一个通用的解决方案。
让我们假设您在每一行中填充的对象如下所示。
public class ListItem {
int value;
boolean highlight = false;
}
ListItem 对象的列表可以插入到ArrayList 中以填充到RecyclerView 中。这是您的适配器,可能看起来像这样。
// Declare the yourListItems globally in your Activity
List<ListItem> yourListItems = new ArrayList<ListItem>();
populateYourListItems();
public class YourAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public class YourViewHolder extends RecyclerView.ViewHolder {
private final TextView valueTextView;
private final LinearLayout background;
public YourViewHolder(final View itemView) {
super(itemView);
valueTextView = (TextView) itemView.findViewById(R.id.value_text_view);
background = (LinearLayout) itemView.findViewById(R.id.background);
}
public void bindView(int pos) {
int value = yourListItems.get(pos).value;
boolean isHighlighted = yourListItems.get(pos).hightlight;
valueTextView.setText(value);
// Set the background colour if the highlight value is found true.
if(isHighlighted) background.setBackgroundColor(Color.GREEN);
else background.setBackgroundColor(Color.WHITE);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_activity_log, parent, false);
return new YourViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
try {
if (holder instanceof YourViewHolder) {
YourViewHolder vh = (YourViewHolder) holder;
vh.bindView(position);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
if (yourListItems == null || yourListItems.isEmpty())
return 0;
else
return yourListItems.size();
}
@Override
public int getItemViewType(int position) {
return 1;
}
}
现在,当您要突出显示 RecyclerView 的某些特定项目时,只需将 highlight 值设置为 true,然后调用 notifyDataSetChanged() 即可使更改生效。
因此,您可能需要一个像下面这样的计时器,它会根据您的需求每两秒突出显示您的行。
// Declare the timer
private Timer highlightTimer;
private TimerTask highlightTimerTask;
highlightTimer = new Timer();
highlightTimerTask = new TimerTask() {
public void run() {
highLightTheListItems();
}
};
highlightTimer.schedule(highlightTimerTask, 2000);
现在根据您的需要实现您的highLightTheListItems 函数。
public void highLightTheListItems() {
// Modify your list items.
// Call notifyDataSetChanged in your adapter
yourAdapter.notifyDataSetChanged();
}
希望对您有所帮助。谢谢。