I have a RecyclerView
and clicking its item takes to a new Activity
. There is a ViewPager
with multiple items. On pressing back, I am saving the current item position. What I want to do is when a user presses back while standing on any item in a ViewPager
, that item should be highlighted in RecyclerView
.
PROBLEM
When item's position is visible, there's no problem. I change background by using following
view = layoutManager.findViewByPosition(Utils.ayat);
if(view != null) {
LinearLayout parent = (LinearLayout) view.findViewById(R.id.parent);
parent.setBackgroundColor(Color.GRAY);
}
But if item is not visible, nothing happened and I am not able to change background.
WHAT I'VE DONE SO FAR
- Tried to scroll to a position and then change background.
RecyclerView
is scrolling correctly butfindViewByPosition
returns null. - I believe from my observations (may be it's wrong) that on programmatically scrolling
RecyclerView
, the position to which it's been scrolled will be it's last visible item. I tried to do that but with no luck. - As suggested by many, put
Thread.sleep(100)
and then tried to get position. This didn't work too.
NOTE: I can't do this onBindViewHolder
because items to be highlighted are not pre-defined.