回收者视图:我希望在特定时间间隔后突出显示回收者视图项目(行) [英] Recycler View: I want my recycler view item(rows) to be highlighted after specific interval of time

查看:66
本文介绍了回收者视图:我希望在特定时间间隔后突出显示回收者视图项目(行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want my recycler view rows to be highlighted after a specific interval of time, say after 2 seconds. Searched all over the internet but no luck so far.

推荐答案

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

The question is not very clear. I had two questions in mind that I mentioned in the comment of the question.

  • 您要突出显示某些特定的行吗?
  • 是否要每两秒钟切换一次高光显示?

因此,我将为这两种方法寻求通用解决方案.

So I'm going for a general solution for both.

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

Let us assume the object you're populating in your each row is like the following.

public class ListItem {
    int value; 
    boolean highlight = false;
}

ListItem对象的列表可以插入到ArrayList中,以填充在RecyclerView中.这是您的适配器,看起来可能像这样.

The list of ListItem object can be inserted in an ArrayList to be populated in the RecyclerView. Here is your adapter which may look like this.

// 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()即可生效.

Now when you want to highlight some specific items of your RecyclerView you need to just set the highlight value to true and then call notifyDataSetChanged() to bring into the change in effect.

因此,您可能需要一个类似以下的计时器,该计时器将每两秒钟根据您的需求突出显示您的行.

So you might need a timer like the following which will highlight your rows as per your demand in every two seconds.

// Declare the timer
private Timer highlightTimer;
private TimerTask highlightTimerTask;

highlightTimer = new Timer();
highlightTimerTask = new TimerTask() {
    public void run() {
        highLightTheListItems();
    }
};

highlightTimer.schedule(highlightTimerTask, 2000);

现在根据需要实现您的highLightTheListItems功能.

Now implement your highLightTheListItems function as per your need.

public void highLightTheListItems() {
    // Modify your list items. 
    // Call notifyDataSetChanged in your adapter
    yourAdapter.notifyDataSetChanged();
}

希望有帮助.谢谢.

这篇关于回收者视图:我希望在特定时间间隔后突出显示回收者视图项目(行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆