RecyclerView ins and outs - Google I/O 2016

Android Developers · 98K views · 2016 · 46:10

Open on YouTube

Software (other)Software architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when explaining RecyclerView internals such as binding, position changes, caching, item animators and layout managers, often in answers about adapter and animation bugs.

Recommended by 5 people across 5 different months, 2016 to 2021.

What it is

A Google I/O 2016 talk by the Android team on how RecyclerView works internally. It covers the history of ListView, the design lessons behind RecyclerView, how it handles layouts, animations and UX flows, and best practices.

What people said

Strong rec Sevastyan Savanyuk · 2017-04 · Stack Overflow · on “RecyclerView scroll makes findViewHolderForAdapterPosition return null”

@SaranSankaran, I prefer not to give answers to such easy/beginner type of questions for your own good, because it takes all the fun out of figuring out it yourself. Moreover, I think there are several solutions to your problem. I **highly recommend** watchin I/O talk on RecyclerView. Start from 18:00. I/O presentation [this video]

Recommends Anas Azeem · 2016-10 · Stack Overflow · on “Can we use ItemAnimator to do animation on view's focus change”

I am working on an Android TV app, there I am currently I am doing animation on RecyclerView when the focus on a view changes in the onBindViewHolder of the adapter like this: @Override public void onBindViewHolder(final ViewHolder holder, int position) { ... holder.row.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean hasFocus) { if (hasFocus) { startEnterAnimation(view); } else { startExitAnimation(view); } } }); ... } But this morning I was watching RecyclerView ins and outs - Google I/O 2016 [this video] talk and found that, it is a bad approach to do RecyclerView animations in onBindViewHolder method. Instead ItemAnimator shoul

Recommends Samuel Robert · 2017-12 · Stack Overflow · on “Hover event in RecyclerView using ItemTouchHelper”

LayoutManager takes care of the view positions. Dig deeper into LinearLayoutManager in association with ItemAnimator... There is a great video from the guys who created RecyclerView [this video]

Recommends Ben P. · 2018-08 · Stack Overflow · on “RecyclerView multiple selection deselects the wrong ViewHolders”

This answer is based on the fact that you're doing something I know you're not supposed to do. It's possible that fixing this won't solve your issue, but it's still worth pointing out: The position argument to onBindViewHolder() should not be considered to be final . In other words, a view holder's position can change over time. Your posted code includes this: holder.itemView.setOnClickListener { clickListener.onItemClick(position, it) notifyDataSetChanged() } Imagine your view holder is initially bound for position 14 , and you then insert two rows at the beginning of the list. Your click listener will still call onItemClick(14, it) , but your real position is now 16 . You can solve this by

Recommends valR · 2019-05 · Stack Overflow · on “ItemDecoration based on viewtype in recyclerview”

You should not access the adapter in the decoration, the adapter point of view may not be what is on screen (see google io talk [this video] ), keep all necessary information in the view holder (access it with #getChildViewHolder).

Cites albodelu · 2016-08 · Stack Overflow · on “How can I return data from a RecyclerView adapter?”

Updated response: Alternative using the CarItems list: @OnClick(...) public void onButtonClicked() { int size = ((MyCustomAdapter) mRecyclerView.getAdapter()).getItemCount(); for (int i = 0; i < size; i++) { if (((MyCustomAdapter) mRecyclerView.getAdapter()).isItemChecked(i)) { // Get each selected item CarItem carItem = (MyCustomAdapter) mRecyclerView.getAdapter()).getItem(i); // Do something with the item like save it to a selected items array. } } } You also can add a getCheckedItems() method to the adapter: public List<CarItem> getCheckedItems() { List<CarItem> checkedItems = new ArrayList<>(); for (int i = 0; i < getItemCount(); i++) { if (isItemChecked(i)) { checkedItems.add(getItem(i)

Cites napkinsterror · 2016-08 · Stack Overflow · on “How to make an Android custom view that is compatible with RecyclerView”

It is pretty hard to tell what is going on exactly especially if this code is working elsewhere, but I'll take a couple guesses. The main things I noticed: Comparing int from long where numbers are dangerously close to max Calling Invalidate from a View inside a RecyclerView (especially onBindView) Issue 1 In your picture, I'm guessing you are the steamId which are the numbers on the bottom left corner of each RecyclerView's view holder, for example: '2563966339'. You should know that "usually" in Android, Integer.MAX_VALUE = 2147483647 . This pretty much means you should use long or things won't be equal when you think they are... (so maybe the boxes are being drawn correctly, but you just

Cites Jon Douglas · 2016-11 · Stack Overflow · on “What is the correct moment to call Dispose() when using the ViewHolder pattern?”

ListView is pretty old(Android 1.0). It was tightly coupled and not built with performance in mind. Lots of hacks were needed to keep it relevant. RecyclerView fills that gap. [this video] As for when you should call Dispose() , RecyclerView should handle this basic functionality via the LayoutManager . On the ViewHolder side, you can follow a basic Dispose pattern: protected override void Dispose (bool disposing) { base.Dispose (disposing); if(ItemView != null) { ItemView.Click -= HandleClick; } _listener = null; //Listener might just be a simple Action<int> like in this example: [LINK] } In which you only care about Disposing the base and any Event Handlers you set up. However if your RecyclerV

Cites Salauddin Gazi · 2017-01 · Stack Overflow · on “Android: access any specific row of recyclerview on interaction with other row”

public class HelloActivity extends Activity { private RecyclerView mRecyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); mRecyclerView.setAdapter(new HelloAdapter()); DefaultItemAnimator animator = new DefaultItemAnimator() { @Override public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder) { return true; } }; mRecyclerView.setItemAnimator(animator); } private static class HelloAdapter extends RecyclerView.Adap

Cites Ben P. · 2018-02 · Stack Overflow · on “RecyclerView notifyItemChanged(position) crashes the app and no animations with notifyDataSetChanged()”

There are two problems: Don't call notifyItemChanged() after calling notifyItemRemoved() Calling notifyItemRemoved() will tell the adapter that the item previously at position doesn't exist anymore, and that's all you need to do. Calling notifyItemChanged() tells the adapter that the item at position has been modified (perhaps its name changed, but nothing else), so it's not meant for use when you delete an item. You're getting a crash because you call both of these in order. Imagine that you only have one item in your list, and then delete it. When you call notifyItemRemoved() , the adapter now knows that your list has zero items in it. But then you call notifyItemChanged() , and so the ada