I\'m trying to use a keyboard to navigate through a GridView of items.
In the short demo, which only contains a GridView (containing item views with android:fo
I tried your code and it doesn't work with GridView, as you said, but i have some issue also with ListView
. The ListView
scrolls correctly, but only the first item of every page is highlighted.
With these little modifications you can achieve the effect you want on both GridView and ListView.
Remove android:descendantFocusability
in activity_my.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2" />
Remove android:focusable
in dummy_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dummy_item_parent"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center"
android:clickable="true"
android:background="@drawable/item_background" />
Change from android:state_focused
to android:state_selected
in item_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@android:color/holo_blue_light" />
<item android:state_selected="true" android:drawable="@android:color/holo_orange_light" />
<item android:drawable="@android:color/holo_red_light" />
</selector>