× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

RecyclerView - how can I get a reference to each row's View

Xaver Kapeller
Mar 13, 2015
<p>Every view holder used by the <code>RecyclerView</code> has to subclass <code>RecyclerView.ViewHolder</code> and this class has a public field called <code>itemView</code> which contains the <code>View</code> associated with the <code>ViewHolder</code>.</p> <pre><code>final View view = viewHolder.itemView; </code></pre> <p>But just in general: Animations work differently in the <code>RecyclerView</code>. Essentially you implement them in a separate class called <code>ItemAnimator</code>:</p> <pre><code>mRecyclerView.setItemAnimator(new RecyclerView.ItemAnimator() { @Override public void runPendingAnimations() { } @Override public boolean animateRemove(RecyclerView.ViewHolder viewHolder) { return false; } @Override public boolean animateAdd(RecyclerView.ViewHolder viewHolder) { return false; } @Override public boolean animateMove(RecyclerView.ViewHolder viewHolder, int i, int i2, int i3, int i4) { return false; } @Override public boolean animateChange(RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder viewHolder2, int i, int i2, int i3, int i4) { return false; } @Override public void endAnimation(RecyclerView.ViewHolder viewHolder) { } @Override public void endAnimations() { } @Override public boolean isRunning() { return false; } }); </code></pre> <p>In all those callbacks you can access the <code>View</code> instance through the public field as I explained above. The <code>RecyclerView.Adapter</code> unlike the previous <code>Adapters</code> is really just responsible for the data in the <code>RecyclerView</code>.</p> <p>You can also look at <a href="https://github.com/gabrielemariotti/RecyclerViewItemAnimators" rel="nofollow"><strong>this library on GitHub</strong></a> which already implements a few different <code>ItemAnimators</code>.</p> <p>Please see the documentation for more information!</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/27206593/RecyclerView%20-%20how%20can%20I%20get%20a%20reference%20to%20each%20row's%20View/27206626">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus