【问题标题】:Recycler View: I want my recycler view item(rows) to be highlighted after specific interval of time回收站视图:我希望在特定时间间隔后突出显示我的回收站视图项目(行)
【发布时间】:2017-09-29 00:49:18
【问题描述】:

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

【问题讨论】:

  • 问题不是很清楚。您想突出显示RecyclerView 中的某些特定行吗?你想每隔两秒切换一次高亮显示吗?
  • @ReazMurshed 我希望我的行在 2 秒后一一突出显示。在前 2 秒内,第一行突出显示,然后下一行继续。
  • 请看我的回答。它会达到你的目的。

标签: android android-recyclerview


【解决方案1】:

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

在您传入处理程序的运行器中,您放入一个 布尔标志 以检查该行是否将具有不同的颜色/行为 + 适配器中的 notifyDataSetChanged()。 (您必须更改数据对象以适应这个新变量)

【讨论】:

    【解决方案2】:

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

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

    所以我要为这两者寻找一个通用的解决方案。

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

    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();
    }
    

    希望对您有所帮助。谢谢。

    【讨论】:

    • 我正在实施您的解决方案。你能告诉我在哪里写定时器代码吗?
    • 请在你的onCreate函数中做,如果你有任何其他问题请告诉我。
    【解决方案3】:

    你的意思是高亮显示为行背景的颜色吗?如果是这样,您可以在您的 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      相关资源
      最近更新 更多