使用dpad导航listview项目

时间:2016-10-10 11:32:15

标签: listview keyevent android-tv d-pad

我正在为Android电视开发一个应用程序,其中我有一个listview。我想使用D-pad在listview项目之间导航。

例如,如果使用按下向下箭头按钮,则下一个列表项应获得焦点。我创建了一个简单的列表视图,但我真的需要指导显示D-Pad并在项目之间滚动。非常感谢提前。

listView = (ListView) findViewById(R.id.list);
listView.setItemsCanFocus(true);
String[] values = new String[]{"Android List View",
            "Adapter implementation",
            "Simple List View In Android",
            "Create List View Android",
            "Android Example",
            "List View Source Code",
            "List View Array Adapter",
            "Android Example List View",
            "Adapter implementation",
            "Simple List View In Android",
            "Create List View Android",
            "Android Example List View"
    };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter);

    listView.setOnItemSelectedListener(new adapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            Log.e("right button", "yes");
            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            Log.e("right button", "yes");
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            Log.e("right button", "yes");
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            Log.e("right button", "yes");
            break;

        default:
            break;
    }
    return super.onKeyDown(keyCode, event);
}

}

0 个答案:

没有答案