Keyboard navigation with Android GridView doesn't scroll grid

前端 未结 1 860
半阙折子戏
半阙折子戏 2021-01-07 23:34

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

相关标签:
1条回答
  • 2021-01-08 00:12

    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>
    
    0 讨论(0)
提交回复
热议问题