List View Focus While Using D-pad
Solution 1:
Make a dummy view without background and place it in the same layout as your listview. Make it focusable and set nextFocusRight of your Search view to id/dummy.
<ListViewandroid:id="@+id/listview"android:layout_width="match_parent"android:layout_height="wrap_content"android:choiceMode="singleChoice"android:listSelector="@null"android:background="@null"
></ListView><Viewandroid:id="@+id/dummy"android:layout_width="1dp"android:layout_height="1dp"android:focusable="true"
/>
Your searchView:
<YourSearchView
android:nextFocusItemRight=@+id/dummy/>
Then in the code, set an OnFocusChangedListener to the dummy view which will indicate that the ListView is supposed to gain focus. Inside the listener, request focus for the listview and set selection to 0 as follows:
View dummy = rootView.findViewById(R.id.dummy);
dummy.setOnFocusChangeListener(newView.OnFocusChangeListener()
{
@OverridepublicvoidonFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
listView.post(newRunnable() {
@Overridepublicvoidrun() {
listView.requestFocus();
listView.setSelection(0);
}
});
}
}
});
Make sure you use @+id for nextFocusItemRight instead of @id. Hope it helps, otherwise post a comment.
Solution 2:
The best thing to do is Override the dispatchKeyevent in your Activity class then set the focus/selection to the place wherever you like in Listview .
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean handled = super.dispatchKeyEvent(event);
if(!handled && event.getAction() == KeyEvent.ACTION_UP)
{
if(yourParentViewGroup.getFocusedChild() is your SearchView && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT )
{
yourListvIew.setSelction(0);
handled = true;
}
}
return handled;
But remember to return "true" if you are going to handle the Focus or selection logic else it will invoke android focusfinder algorithm and focus/selection will shift to the nearest View.
Post a Comment for "List View Focus While Using D-pad"