如何在 Robolectric 中测试定向聚焦导航?

问题描述

我试图限制焦点如何离开某个 View。使用定向焦点,我只想通过向左或向右,而不是向上或向下离开 View。我已经使用 android:nextFocusUpandroid:nextFocusDown 指向 View 本身来完成此操作:

<MyView
  android:id="@+id/my_id"
.
.
.
  android:nextFocusUp="@id/my_id"
  android:nextFocusDown="@id/my_id"
  android:focusable="true"
  android:clickable="true"/>

我想通过将我的视图放置在 3x3 GridLayout 的中心并检查焦点只能向左和向右移动,而不是向上和向下移动来测试这一点。

>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:columnCount="3"
    android:rowCount="3"
    android:descendantFocusability="afterDescendants">
  <View
      android:id="@+id/top_left"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:focusable="true"
      />
  <View
      android:id="@+id/top_middle"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:focusable="true"
      />
  <View
      android:id="@+id/top_right"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:focusable="true"
      />
  <View
      android:id="@+id/middle_left"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:focusable="true"
      />
  <MyView
      android:id="@+id/true_middle"
      android:layout_width="50dp"
      android:layout_height="50dp"
      />
  <View
      android:id="@+id/middle_right"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:focusable="true"
      />
  <View
      android:id="@+id/bottom_left"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:focusable="true"
      />
  <View
      android:id="@+id/bottom_middle"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:focusable="true"
      />
  <View
      android:id="@+id/bottom_right"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:focusable="true"
      />
</GridLayout>

我遇到的问题是,当我使用 {{{{ 1}}、FocusFinder#findNextFocusView#focusSearchView.FOCUS_LEFT。唯一有效的是 View.FOCUS_RIGHTView.FOCUS_UP

View.FOCUS_DOWN

最后两行中的任何一行都会使断言失败。

如何在 Robolectric 中测试定向焦点导航?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...