使用 Android GridView 的键盘导航不会滚动网格 [英] Keyboard navigation with Android GridView doesn't scroll grid
问题描述
我正在尝试使用键盘在项目的 GridView 中导航.
I'm trying to use a keyboard to navigate through a GridView of items.
在简短的演示中,它只包含一个 GridView(包含带有 android:focusable="true"
的项目视图),你可以看到没有一个项目获得焦点 - 网格就是东西滚动(项目不会变成橙色).
In the short demo, which only contains a GridView (containing item views with android:focusable="true"
), you can see that none of the items gain focus - the grid is the thing that scrolls (the items don't turn orange).
添加 android:descendantFocusability="afterDescendants"
允许首先聚焦每个项目,但仍然不会继续滚动 GridView,因此您可以向下导航:
Adding the android:descendantFocusability="afterDescendants"
allows each item to be focused first, but still, doesn't continue to scroll the GridView so you can navigate down:
单击一个项目(使用鼠标或按下 Return 键,如果该项目已聚焦,在后代聚焦性修复后)会将项目变成蓝色 - 表示它处于按下状态.
Clicking on an item (with mouse or by pressing Return if the item was focused, after the descendent focusability fix) turns an item blue - indicating it's in pressed state.
@layout/activity_my.xml:
@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:
@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:
@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(带有 ListAdapter):
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;
}
}
}
将 GridView 交换为 ListView,而不更改适配器或项目视图/项目视图属性,将按预期运行 - 项目可以聚焦并且 ListView 将滚动到底部,以便所有项目都可以使用只是键盘输入.此外,将 RecyclerView 与 LinearLayoutManager/GridLayoutManager 一起使用也可以正常工作.
Swapping the GridView for a ListView, without changing the adapter, or the item views/item view attributes, will behave as expected - items can be focused and the ListView will scroll to the bottom, so that all items can be focused using just keyboard input. Also, using a RecyclerView with LinearLayoutManager / GridLayoutManager will also work correctly.
我已经在 API 16 和 22 上尝试过,结果相同.我已经尝试将 GridView 上的 android:descendantFocusability
属性设置为 afterDescendents
,结果相同.
I have tried this on API 16 and 22 with the same results. I have tried settings the android:descendantFocusability
attribute on the GridView to afterDescendents
with the same result.
推荐答案
我尝试了您的代码,但它不适用于 GridView,正如您所说,但我对 ListView
也有一些问题.ListView
滚动正确,但只突出显示每个页面的第一项.
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.
通过这些小的修改,您可以在 GridView 和 ListView 上实现您想要的效果.
With these little modifications you can achieve the effect you want on both GridView and ListView.
删除activity_my.xml中的android:descendantFocusability
:
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" />
删除 dummy_item.xml 中的 android:focusable
:
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" />
在 item_background.xml 中从 android:state_focused
更改为 android:state_selected
:
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>
这篇关于使用 Android GridView 的键盘导航不会滚动网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!