我在猜測GTV團隊已經在GoogleTV操作系統的View Hierarchy中添加了一些特殊的醬料,因爲這個神奇的方法在LeftNavBar的任何一個孩子獲得焦點時以某種方式被調用。
protected void onDescendantFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (has(mDisplayOptions, LeftNavBar.DISPLAY_AUTO_EXPAND)) {
setExpanded(hasFocus);
}
}
我看不出模仿這種使用其他方法,所以我下面執行黑客。
1)在onFinishInflate()的LeftNavView.java末尾添加以下代碼時,底層的ListView獲得焦點,這樣就可以展開菜單來檢測。遺憾的是它並不像使用hasFocus == false,以可靠地關閉菜單那麼簡單,所以請參閱步驟2
@Override
protected void onFinishInflate() {
//...
//existing code
//...
TabListView tabListView = (TabListView)mTabs.getView();
tabListView.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
setExpanded(true, true);
}
});
}
2)添加下面的focusSearch(布爾hasFocus)在LeftNavView.java的頂部塌陷菜單(如果LeftNavBar已有焦點和DPAD方向是正確的
@Override
public View focusSearch(View focused, int direction) {
//if leftnav hasFocus but direction is right then collapse the nav menu
if (hasFocus() && direction == View.FOCUS_RIGHT) {
setExpanded(false, true);
}
//...
//existing code
//...
}
警告:有了這個本事,你需要小心的是你沒有加載時(或者調用該請求將焦點的觀點。 requesFocus()或者在你的佈局中實現requestFocus />標籤)。在這種情況下,你的視圖會獲得焦點,但菜單仍然會被擴展,在某些情況下,你會你將無法使用dpad將注意力集中在菜單上(取決於視圖的佈局)
如果您想要將注意力集中在新加載的片段中,那麼您可以實現並調用下面的方法(在onActivityCreated()的結尾處)作爲解決方法。
//e.g. passing in KeyEvent.KEYCODE_DPAD_RIGHT
public static void simulateKey(final int KeyCode) {
new Thread() {
@Override
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyCode);
} catch (Exception e) {
Log.e(TAG, "Exception when sendKeyDownUpSync " + e.toString());
}
}
}.start();
}
Android 4.x上的leftnavbar存在問題。它只在GTV Honeycomb OS上擴展和崩潰,但不在JB OS上。 因此,在GTV將更新至v.4之後,leftnavbar將會按預期停止工作。它需要修復。 – mcd2000 2013-09-09 09:58:51