TheQ
TheQ

Reputation: 2019

Change the recyclerview's focus to certain item position?

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!

Upvotes: 3

Views: 23934

Answers (3)

Elie Danhash
Elie Danhash

Reputation: 61

just use this

recyclerView.scrollToPosition(position);

Upvotes: 1

Avi
Avi

Reputation: 561

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.

Upvotes: 1

Ajeet Yadav
Ajeet Yadav

Reputation: 691

recyclerView.setAdapter(adapter);// set adapter on recyclerview
recyclerView.scrollToPosition(mSkipTo); //use to focus the item with index
adapter.notifyDataSetChanged();
mProgressDialog.dismiss();

Upvotes: 13

Related Questions