Recycler View:我希望在特定的时间间隔

时间:2017-09-28 15:54:07

标签: android android-recyclerview

我希望我的回收者视图行在特定的时间间隔后突出显示,比如2秒后。 在互联网上搜索但到目前为止没有运气。

3 个答案:

答案 0 :(得分:2)

如何在回收器适配器OnBindViewHolder方法中放置一个[Handler.postDelayed](https://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable,long)),在其中设置您希望项目更改的具体时间。

在传递给处理程序的运行器内部,您输入布尔标志以检查该行是否在适配器中具有不同的颜色/行为+ notifyDataSetChanged()。 (您必须更改数据对象以容纳此新变量)

答案 1 :(得分:1)

问题不是很清楚。我在问题的评论中提到了两个问题。

  • 您想突出显示某些特定行吗?
  • 您想在每两秒钟之后切换亮点吗?

所以我要为两者寻求一般解决方案。

让我们假设您在每行中填充的对象如下所示。

MimeEntity ParseMultipartFormData (HttpWebResponse response)
{
    var contentType = ContentType.Parse (response.ContentType);

    return MimeEntity.Parse (contentType, response.GetResponseStream ());
}

可以在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

希望有所帮助。感谢。

答案 2 :(得分:0)

你的意思是突出显示行的背景颜色吗?如果是这样,您可以在listViewAdapter中执行此操作

@Override
public View getView(final int position, View row, ViewGroup parent){
if (row==null){
    row = LayoutInflater.from(getContext()).inflate(mResource, parent, false);
}

if(foo){
    row.setBackgroundColor(getResources().getColor(R.color.translucent_green));
}
else row.setBackgroundColor(Color.TRANSPARENT);

return row;
}

然后在colours.xml

<color name="translucent_green">#667cfc00</color>

前2个数字(66)是alpha值,即不透明度。接下来的6个是十六进制的RBG。