微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在 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 举报,一经查实,本站将立刻删除。