1

我正在为 android tv 开发一个应用程序,其中我有一个 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);
}

}

4

0 回答 0