这里VerticalGridView(VG)将填充一些数据。 VG专注于向上/向下按D-PAD键,它工作正常。但是问题是,当焦点位于网格中的第零项并且用户按下D-PAD键时,焦点必须设置为VG之外的视图之一,即ll_exit(xml文件中提到的id),但当前焦点不是前往上述观点。请让我知道如何解决这个问题。
我尝试设置android:nextFocusUp =“ @ id / ll_exit”,但它没有移动,并尝试在活动级别上实现如下所示的onKeyUp方法
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
Log.d(TAG, "onKeyUp: " + event.getAction());
GenresFragment fragment = (GenresFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_genre);
int pos = fragment.getVerticalGridView().getSelectedPosition();
if (pos == 0) {
fragment.getLlExit().requestFocus();
}
return true;
default:
return super.onKeyUp(keyCode, event);
}
}
但是这里的问题是当我在VG中的第一项并且突然按下向上键时,它会转到我需要聚焦的视图。这就是xml文件中的ll_exit视图
<b>main_layout.xml</b>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_toolbar"
android:layout_width="@dimen/select_genre_fragment_width"
android:layout_height="@dimen/select_genre_toolbar_height"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_all_channels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/ll_genres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/ll_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<android.support.v17.leanback.widget.VerticalGridView
android:id="@+id/vg_genres"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/ll_toolbar"
android:nextFocusUp="@id/ll_exit" />
</RelativeLayout>
答案 0 :(得分:0)
尝试将以下XML属性添加到LinearLayout视图ll_exit
android:focusable="true"
android:focusableInTouchMode="true"
答案 1 :(得分:0)
可能的方式: 尝试将LinearLayout ll_toolbar设置为
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="afterDescendants"
或尝试:
View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_UP);
if (nextFocused != null) {
nextFocused.requestFocus();
}
如果没有解决方案,请像这样扩展VerticalGridView
public class SampleVerticalGridView extends VerticalGridView implements FocusInterceptor{
private static final String TAG = "SampleVerticalGridView";
private View focusUp;
private View focusDown;
// constructors and stuff
...
public void setNextFocusViews(View focusUp, View focusdown) {
this.focusUp = focusUp;
this.focusdown = focusdown;
}
@Override
public View focusSearch(View focused, int direction) {
if (direction == View.FOCUS_UP && focusUp != null) {
return focusUp;
} else if (direction == View.FOCUS_DOWN && focusDown != null) {
return focusDown;
}
return super.focusSearch(focused, direction);
}
...
}
答案 2 :(得分:0)
尝试将以下XML属性添加到VG
app:focusOutEnd="true"
app:focusOutFront="true"
对我有用!