I\'ve been using SwipeableRecyclerView for my android application for enabling swipes for my recyclerView. RecyclerView contains a list of cardViews.
I was trying to
Since I don't have enough reputation points to comment on @yigit's answer, I thought I would give a way to retrieve all viewHolders from a RecyclerView regardless of what is currently in the viewGroup. Even with findViewHolderForAdapterPosition and ForLayoutPosition we still can get a null instance of the ViewHolder in the RecyclerView.
To circumvent this, we can use findViewHolderForItemId[1] and pass the ID of the viewHolder in the adapter using RecyclerView.Adapter#getItemId(int position)[2]
for (int i = 0; i < mAdapter.getItemCount(); i++) {
RecyclerView.ViewHolder holder =
mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(i));
}
Use findFirstCompletelyVisibleItemPosition() & findLastVisibleItemPosition() methods on LayoutManager of your RecyclerView to find out that the child you are looking for is visible or not. If it is visible then you can use getChild(index) to get the ViewHolder instance of that particular child and it wouldn't be null else it could be null(If the child is not in it). In case your child is not visible you just make changes in your specific position List object, next time whenever it'll be visible will have updated UI.
if (selectedPosition >= linearLayoutManager.findFirstVisibleItemPosition()
&& selectedPosition <= linearLayoutManager.findLastVisibleItemPosition()){
linearLayoutManager.getChildAt(selectedPosition).setBackgroundResource(0);
}
You should use findViewHolderForAdapterPosition
.
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#findViewHolderForAdapterPosition(int)
Keep in mind that it will return null if the position is not laid out (e.g. out of bounds or removed).