Keyboard navigation with Android GridView doesn't scroll grid -


i'm trying use keyboard navigate through gridview of items.

in short demo, contains gridview (containing item views android:focusable="true"), can see none of items gain focus - grid thing scrolls (the items don't turn orange).

scrolling short demo

adding android:descendantfocusability="afterdescendants" allows each item focused first, still, doesn't continue scroll gridview can navigate down:

with descendent focusability set

clicking on item (with mouse or pressing return if item focused, after descendent focusability fix) turns item blue - indicating it's in pressed state.

clicking grid items

@layout/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" /> 

@layout/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:focusable="true"   android:clickable="true"   android:background="@drawable/item_background" /> 

@drawable/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_focused="true" android:drawable="@android:color/holo_orange_light" />   <item android:drawable="@android:color/holo_red_light" /> </selector> 

myactivity.java (with listadapter):

public class myactivity extends activity {      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_my);          abslistview listview = (abslistview) findviewbyid(r.id.listview);         listview.setadapter(new dummyadapter(getlayoutinflater()));     }      private static class dummyadapter extends baseadapter {          private static final int count = 25;          private final layoutinflater layoutinflater;          dummyadapter(layoutinflater layoutinflater) {             this.layoutinflater = layoutinflater;         }          @override         public int getcount() {             return count;         }          @override         public string getitem(int position) {             return "item " + position;         }          @override         public long getitemid(int position) {             return position;         }          @override         public view getview(int position, view convertview, viewgroup parent) {             view view = convertview;             if (view == null) {                 view = layoutinflater.inflate(r.layout.dummy_item, parent, false);             }             ((textview) view).settext(getitem(position));             return view;         }      }  } 

swapping gridview listview, without changing adapter, or item views/item view attributes, behave expected - items can focused , listview scroll bottom, items can focused using keyboard input. also, using recyclerview linearlayoutmanager / gridlayoutmanager work correctly.

i have tried on api 16 , 22 same results. have tried settings android:descendantfocusability attribute on gridview afterdescendents same result.

i tried code , doesn't work gridview, said, have issue listview. listview scrolls correctly, first item of every page highlighted.

with these little modifications can achieve effect want on both gridview , 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 android:state_focused 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> 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -