Husnain
Husnain

Reputation: 137

Recycler View: I want my recycler view item(rows) to be highlighted after specific interval of time

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.

Upvotes: 3

Views: 1093

Answers (3)

Reaz Murshed
Reaz Murshed

Reputation: 24211

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

  • Do you want to highlight some specific rows?
  • Do you want to toggle the highlight after each two seconds?

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

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

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

Now implement your highLightTheListItems function as per your need.

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

Hope that helps. Thanks.

Upvotes: 1

AD S2dios
AD S2dios

Reputation: 41

Do you mean highlight as in colour the row's background? If so, you could do this in your 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;
}

Then in colours.xml

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

The first 2 numbers(66) is the alpha value, ie opacity. The next 6 are RBG in hexadecimal.

Upvotes: 0

Vanethos
Vanethos

Reputation: 791

How about, in the recycler adapter OnBindViewHolder method, put a [Handler.postDelayed](https://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)) in which you set the specific time where you want the item to change.

Inside the runner that you pass in the handler, you put in a boolean flag to check if the row will have a different colour/behaviour + a notifyDataSetChanged() in the adapter. (You will have to change your data object to accomodate this new variable)

Upvotes: 3

Related Questions