0

There is some code snippet in my project which used RecycleView.

Is there any way to let the first visible item to get the focus rather than the last visible item?

// The picture marked with RED
parentRecycleView.setAdapter(ParentAdapter)
// The picture marked with GREEN
parentRecycleViewViewHolder.populate(xxxx){
   childRecycleView.setAdapter(ChildAdapter)
}

Then I scroll to the first item.

parentRecycle.scrollToPosition(0)

As you can see in the capture below: the last visible one got the focus(EditText) auto.

My Question is: Is there any to let the first visible item to get focus(EditText)?

The image capture

Peng Cao
  • 1
  • 1

1 Answers1

0

To get the first visible item First visible child depends on the LayoutManager. If you are using LinearLayoutManager or GridLayoutManager, you can use

int findFirstVisibleItemPosition(); int findFirstCompletelyVisibleItemPosition(); For example:

GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager()); int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition(); For LinearLayoutManager, first/last depends on the adapter ordering. Don't query children from RecyclerView; LayoutManager may prefer to layout more items than visible for caching.

Now once you have the visible item you can send the focus using requestfocus() function.

And that's it.

Hope it helps.

Thank you!