4

Using Android's RecyclerView, I would like to change the background of a given Item (even if it isn't currently visible).

All the tutorials I saw shows how to access a view using a OnItemClickListener, but in my case that has to be done programmatically without any action from the user. I simply want to retrieve the view, and being able to apply functions such as .setBackground() or findViewById() on it.

How can I achieve this, having the position of the view i want to edit ?

Irshu
  • 8,248
  • 8
  • 53
  • 65
Scaraux
  • 3,841
  • 4
  • 40
  • 80
  • 1
    How will you know which view you want to update? Every other view? Every view with "Jim"? Please add some more details. – CaptJak Apr 24 '16 at 17:16

4 Answers4

2

You can get itemView with RecycleView's LayoutManager:

mRecycleView.getLayoutManager().findViewByPosition(item_position)
Irshu
  • 8,248
  • 8
  • 53
  • 65
Brian Hoang
  • 1,111
  • 9
  • 18
  • 1
    What if the view isn't currently visible ? Will it work ? – Scaraux Apr 24 '16 at 17:00
  • An Exception will be throught – Brian Hoang Apr 24 '16 at 17:03
  • Then how can I update the view so when it will be visible, its background will have changed – Scaraux Apr 24 '16 at 17:04
  • I don't not know what do you want to do with items. If you want set item's background by position, you can do it on adapter. If you want to change item's background for special case, you can add setOnScrollListener for your recycleview and in onScrolled method, you can get visible items, read this post: http://stackoverflow.com/a/25053500/4097058 – Brian Hoang Apr 24 '16 at 17:18
  • Here's what I want to do : I am displaying items related to some users. After 3 minutes, each inactive user is moved to the end of the recycler view and I call `notifyItemMoved()`. After the item position has changed, I would like to be able to change it's background to a darker one, to show it is inactive. – Scaraux Apr 24 '16 at 18:08
  • With me, i will add properties for User object called isActive (boolean), in adapter, check isActive = false, set item background to dark. – Brian Hoang Apr 24 '16 at 18:12
2

Change whatever property of the data item that is backing your RecyclerView that is used to set the background and call notifiyDataSetChanged().

Ivan Wooll
  • 4,145
  • 3
  • 23
  • 34
2

In order to modify the background of a list item in the RecyclerView, you'll have to do the following:

  1. modify the RecyclerView.ViewHolder so it will change the background of the view according to the data passed to it in RecyclerView.Adapter.onBindViewHolder.

  2. modify RecyclerView.Adapter.onBindViewHolder to pass data to the RecyclerView.ViewHolder according to how dark the background should be.

  3. call notifyDataSetChanged() on your RecyclerView.Adapter to update the RecyclerView on the GUI.

Eric
  • 16,397
  • 8
  • 68
  • 76
  • It know works, but do you know how can I wait for an animation to finish, before attempting to modify the item ? The `move`animation is executed at the same time and modifying the background color makes a weird animation. – Scaraux Apr 25 '16 at 08:14
  • I can think of one very quick and simple solution, but isn't doing exactly what you are asking for. The result of this solution is instead of seeing the item change background color and then move to the bottom of the list, you will see the original item fade out, then see a new item with the updated background color fade in at the bottom...so this solution involves: calling `RecyclerView.Adapter.hasStableIds(true)`, then modifying `RecyclerView.Adapter.getItemId()` to return different value based on item data, including its background color. it is ok for method to not return truly unque values – Eric Apr 25 '16 at 08:26
  • This solution that may give you a result that may produce results that better aligns with your request is as follows: perhaps you may use the `RecyclerView.setLayoutAnimationListener()`, and override the `Animation.AnimationListener.onAnimationEnd()` method to change the background color of the backing data at that point instead of modifying it right away, but that seems like it may become complicated....please tell me how it goes... – Eric Apr 25 '16 at 08:30
  • 1
    Thank you for these solutions. The thing is that I would like to see the `item` moving down my view, flying over all the other items. Right now, this works perfectly, using https://github.com/wasabeef/recyclerview-animators library. I'll try to override `onAnimationEnd()`, as you said, it seems to be a good idea – Scaraux Apr 25 '16 at 08:44
  • no problem. that seems like a very nifty library, I might just have to add that to my own projects as well. you may have already stumbled across [this stackoverflow question about detecting when recyclerview animations end](http://stackoverflow.com/questions/33710605/detect-animation-finish-in-androids-recyclerview), but i've linked it anyway. good luck!! – Eric Apr 25 '16 at 09:03
  • 1
    Yeah that one is pretty cool. Thanks for the link :), and thanks for the help – Scaraux Apr 25 '16 at 09:08
0
            for(int i =0; i < mRecyclerView2.getAdapter().getItemCount(); i++) {
                mRecyclerView2.findViewHolderForAdapterPosition(i).itemView.setRotation(-90);
            }

idea spawned from: https://stackoverflow.com/a/41199130/1815624

CrandellWS
  • 2,708
  • 5
  • 49
  • 111