Tôi đang cố gắng sử dụng bàn phím để điều hướng qua GridView của các mục. Trong bản demo ngắn, chỉ chứa một GridView (chứa mục xem với android:focusable="true"
), bạn có thể thấy rằng không có mục nào lấy tiêu điểm - lưới là thứ cuộn (các mục không chuyển sang màu cam).Điều hướng bằng bàn phím với Android GridView không cuộn lưới
Thêm android:descendantFocusability="afterDescendants"
cho phép từng hạng mục được tập trung đầu tiên, nhưng vẫn còn, không tiếp tục di chuyển các GridView để bạn có thể điều hướng xuống:
Nhấp vào một mục (bằng chuột hoặc bằng cách bấm Return (Quay lại) nếu mục đã được lấy nét, sau khi sửa lỗi lấy nét con cháu), hãy chuyển một mục màu xanh dương - cho biết nó ở trạng thái được nhấn.
@ 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>
MyA ctivity.java (với 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;
}
}
}
Trao đổi GridView cho một ListView, mà không thay đổi adapter, hoặc các quan điểm mục/xem mục thuộc tính, sẽ cư xử như mong đợi - mặt hàng có thể được tập trung và ListView sẽ di chuyển ở phía dưới, để tất cả các mục có thể được tập trung chỉ bằng cách nhập bàn phím. Ngoài ra, sử dụng RecyclerView với LinearLayoutManager/GridLayoutManager cũng sẽ hoạt động chính xác.
Tôi đã thử điều này trên API 16 và 22 với cùng kết quả. Tôi đã thử cài đặt thuộc tính android:descendantFocusability
trên GridView thành afterDescendents
với cùng một kết quả.
Tôi sẽ sử dụng RecyclerView với GridLayoutManager (và ItemDecoration, có tính đến khoảng cách mục, để khoảng cách giữa các mục) để triển khai màn hình như vậy. Câu hỏi này chỉ dành cho sự tò mò! – ataulm
Bạn đã thử 'android: drawSelectorOnTop =" true "'? – Manu
@manu cảm ơn bạn đã trả lời. Vấn đề không phải với bộ chọn/tiêu điểm, nó không có khả năng cuộn (bạn có thể thấy trong gif thứ hai, tôi có thể thấy bộ chọn, nhưng vẫn không thể cuộn). – ataulm