为什么Android蓝牙键盘dpad无法使用包含标题视图的listview?

时间:2014-11-10 21:17:38

标签: android listview bluetooth

我有一个列表视图,需要支持通过蓝牙键盘导航。列表视图还有一个标题视图,需要通过键盘导航。问题是,当我按下向下箭头时,焦点将指向列表上方的视图,在此示例中为@ + id / header。我需要按下多次,因为焦点的列表项移动到列表而不是在标题和列表视图的标题之间来回...这很奇怪。

我已经制作了一个示例应用,试图将问题缩小到无济于事。值得注意的是,如果@ + id / header不可聚焦(例如当我将其设为文本视图时),键盘按预期工作,遗憾的是,这对我来说不是一个选项,因为我的标题需要支持蓝牙导航。 / p>

任何帮助都会很棒,谢谢。

MainActivity.java

package com.example.listviewtest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private SimpleAdapter adapter;
    private List<HashMap<String, String>> data;
    private MyListView list;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        list = (ListView) findViewById(R.id.listview);
        TextView empty = (TextView) findViewById(R.id.empty);

        // create the grid item mapping
        String[] from = new String[] {"rowid"};
        int[] to = new int[] { R.id.item1};

        // prepare the list of all records
        data = new ArrayList<HashMap<String, String>>();
        for(int i = 0; i < 10; i++){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("rowid", "" + i);
            data.add(map);
        }

        // fill in the grid_item layout
        adapter = new SimpleAdapter(this, data, R.layout.grid_item, from, to);

        list.addHeaderView(getLayoutInflater().inflate(R.layout.grid_header, null));
        list.setEmptyView(empty);
        list.setItemsCanFocus(true);
        list.setAdapter(adapter);
    }
}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

    <Button 
        android:id="@+id/header"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="Header"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle"
        android:padding="20dp"
        android:layout_marginBottom="20dp"
        android:onClick="toggleList"
        android:clickable="true"
    />

    <!-- ListView (grid_items) -->
    <ListView android:id="@+id/listview"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent" />

    <TextView
        android:id="@+id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="I'm empty"
        android:gravity="center" 
        android:visibility="gone"/>
</LinearLayout>

grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item1"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:padding="20dp" />

grid_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:focusable="false"
    style="?android:attr/buttonBarButtonStyle">

        <TextView android:id="@+id/item1"
            android:text="button1"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:width="0dp"
            android:height="30dip"
            style="?android:attr/buttonBarButtonStyle" />

        <TextView android:id="@+id/item2"
            android:text="button2"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:width="0dp"
            android:height="30dip"
            style="?android:attr/buttonBarButtonStyle" />
</LinearLayout>

0 个答案:

没有答案