Hui.Li
Hui.Li

Reputation: 408

How to control focus in child views in viewpager

I am going to have some windows phone like GUI on Android.

I use ViewPager to implement the page switching effect. It works well except the focus changing.

As image below, when I pressed right key when focus on A1/B2 in page 1, the current page should switch to page 2 and focus should on A2.

All size and place of rects are placed by user, it means that I can not hard code them.

The ViewPager handles page switching very well, but the focus always on the top right when a new page enter.

So, the question is is there any easy way to handle the focus? Or I have to calculate the focus myself by pre-focus coordinates? Thanks!

enter image description here

BTW: The same, when I pressed right key, the focus should change as below:

C1 --> B2
D1 --> C2

while when I pressed left key, the focus should change as below:

A2 --> A1/B2 (both are OK)
B2 --> C1
C2 --> D1
D2 --> D1

Upvotes: 0

Views: 1229

Answers (1)

Hui.Li
Hui.Li

Reputation: 408

I found the answer at last and shared it here.

FocusFinder.getInstance().findNextFocusFromRect supports negative parameters, so what we need to do is call it with rect(A1) - (width,0) and rect(A2).

Upvotes: 1

Related Questions