I'm working on Android TV
application where I have a recycler view
. How can I set focus on wanted position? (I've already tried scrollToPosition
and smoothScroller
, but it doesn't work). Thanks in advance!
Asked
Active
Viewed 4,326 times
5

Jay Rathod
- 11,131
- 6
- 34
- 58

Laura
- 402
- 1
- 7
- 24
1 Answers
5
You have to call the setFocusable(true)
and setFocusableInTouchMode(true)
methods on the item view.
An example:
@NonNull
@Override
public ProgrammeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item_programme, parent, false);
view.setFocusable(true);
view.setFocusableInTouchMode(true);
sDefaultBackgroundColor = ContextCompat.getColor(parent.getContext(), android.R.color.transparent);
sSelectedBackgroundColor = Color.parseColor("#66ffe680");
updateEPGTitleRowBackgroundColor(view, false);
view.setOnFocusChangeListener(mOnFocusChangeListener);
view.setOnKeyListener(mOnKeyListener);
return new ViewHolder(view);
}
Edit:
You can try calling the
mRecyclerView.getChildAt(0).requestFocus();
method to focus a specific item. Just replace the 0 with the item index you want.

burakk
- 1,231
- 2
- 22
- 45
-
Thanks for the answer. I'm already getting the focus on TV and items are focusable. The thing is I want to set focus on certain position when fragment launches. – Laura Dec 14 '18 at 07:22