I want to auto scroll a list item to top (firstVisible item) in my recycler view and get the view at that position, so I can highlight it, from my fragment.
So, this is the gist of my fragment code :
private void activateNewListItem(int position) {
mLayoutManager().scrollToPositionWithOffset(position, 0);
RecyclerView.ViewHolder viewHolder = mRecyclerView.findViewHolderForLayoutPosition(position);
View view = viewHolder.getItemView();
view.setBackgroundColor(getResources().getColor(R.color.esr_light_grey));
}
If position
is 1,2,3,4 etc, mRecyclerView.findViewHolderForPosition(position)
returns a valid ViewHolder,
because I guess RecyclerView has drawn ViewHolders for those indexes in dataSet.
However, if I pass in position
as say, 25, mRecyclerView.findViewHolderForPosition(position)
returns null, because I assume, it hasn't been drawn yet, even thoughI called mLayoutManager.scrollToPositionWithOffset(position, 0)
above it.
What can I do to achieve these two things?
Scroll the list item of dataSet index
position
to firstVisibleItem.Get the View or ViewHolder object of that listItem, so I can change the background or whatever.