3

So I have a standard RecyclerView that takes a list of posts and displays them to the user. My problem is when the user comes into the activity, I want to be able to change the focus to a particular post.

Ex. User A sees that he has a notification that of someone has liked his/her post. He/she clicks that notification, and it loads the activity of user's post, and then the focus changes to the position of the post that had bee liked.

I pass in the postID to the activity using an intent. I then run the Recycler View, and onBindHolder i try to see if the post's ID matches to the postID I sent in via intent. If it does, I now set the activity's variable postFocusPosition to the position of that post.

Now I'm stuck on what that magic method i need to call in RecyclerView class to actually change the focus to that item position!

Hope someone can help enlighten me on that magic line(s) of code that changes focus!

Ahmer Afzal
  • 501
  • 2
  • 11
  • 24
TheQ
  • 1,949
  • 10
  • 38
  • 63
  • Why not use a Scroll to Position? Look at http://stackoverflow.com/questions/26875061/scroll-recyclerview-to-show-selected-item-on-top – Rachit Apr 10 '17 at 05:00

3 Answers3

13
recyclerView.setAdapter(adapter);// set adapter on recyclerview
recyclerView.scrollToPosition(mSkipTo); //use to focus the item with index
adapter.notifyDataSetChanged();
mProgressDialog.dismiss();
Ajeet Yadav
  • 693
  • 1
  • 11
  • 31
1

For smooth scrolling effect and to avoid any resizing issue of recyclerview items, you can use

recyclerView.smoothScrollToPosition(itemposition); //you can get this position, from your adapter class through an interface method

As with "scrollToPosition", I was facing resizing issue of recyclerview items.

Note : You could use this method for both horizontal and vertical recyclerview.

Avi
  • 551
  • 5
  • 8
1

just use this

recyclerView.scrollToPosition(position);
Elie Danhash
  • 61
  • 1
  • 4