android - Espresso : How do I scroll to an item in a HorizontalScrollView by index?

标签 android android-espresso horizontal-scrolling horizontalscrollview

这就是我的 Horizo​​ntalScrollView 的样子:

<HorizontalScrollView
    android:layout_below="@id/saved_circuits_title"
    android:id="@+id/saved_circuits_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:id="@+id/saved_circuits_scroll"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="horizontal">
    </LinearLayout>
</HorizontalScrollView>

在我的 HomeActivity 中,我有以下相关代码,用于使用位图填充 Horizo​​ntalScrollView

onCreate{
...
this.savedCircuitsScroll = (LinearLayout) findViewById(R.id.saved_circuits_scroll);
...
}

updateSavedCircuits(){
...// code to make an ImageView from a retrieved bitmap
newImageImage.setOnClickListener(this.thumbnailListener);
this.savedCircuitsScroll.addView(newImageImage);
...
}

如何使用 Espresso 滚动到 Horizo​​ntalScrollView 中指定索引处的 ImageView 并单击它?


我尝试过的

我的布局 xml 中没有 ID,因此采用 this 等方法不起作用:

onView( withId( R.id.button)).perform( scrollTo(), click());

我知道你可以click on an item by index in a RecyclerView并尝试为 Horizo​​ntalScrollViews 寻找类似的方法:

    onView(withId(R.id.saved_circuits_scroll))
            .perform(HorizontalScrollViewActions.actionOnItemAtPosition(0, click()));

除了 Horizo​​ntalScrollViewActions 不存在。

或关注this blog我尝试了以下操作,至少单击 Horizo​​ntalScrollView 中指定索引处的项目:

// Click item at position 3
onView(withHorizontalScrollView(R.id.scroll_view).atPosition(3)).perform(click());
// Convenience helper
public static HorizontalScrollViewMatcher withHorizontalScrollView(final int horizontalScrollViewId) {
    return new HorizontalScrollViewMatcher(horizontalScrollId);
}

除了 Horizo​​ntalScrollViewMatcher 不存在。

我们为 Horizo​​ntalScrollView 做什么?它不是 ScrollView 的后代,所以答案 here建议我需要实现我自己的自定义ViewAction。我想做的就是按索引滚动到 Horizo​​ntalScrollView 中的某个项目并单击它。这真的有必要吗?如果这是我需要做的,我该如何实现这个自定义 ViewAction


最佳答案

就我而言,我通过使用以下方法得到它:

onView(allOf(withId(R.id.itemTextView), withEffectiveVisibility(Visibility.VISIBLE), withText(R.string.categories))).perform(scrollTo(), click())

R.id.itemTextView 是动态添加到 LinearLayout 的 TextView(带有文本 R.string.categories):

<HorizontalScrollView>
  <LinearLayout>
      ... [child added dynamically]
  </LinearLayout>
</HorizontalScrollView>

关于android - Espresso : How do I scroll to an item in a HorizontalScrollView by index?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40903900/

相关文章:

html - 水平滚动不起作用

android - 使用后台服务开发 Activity 的最佳实践

android - 在使用 Espresso 进行测试时,如何登录我的应用程序一次然后执行所有测试套件?目前,应用程序会为每个测试重新启动

android - 将 ViewModelProvider.Factory 的提供者注入(inject) esspresso 测试

android - Espresso 执行点击

html - 仅当通过 HDMI 电缆连接到电视时,使用负边距的 Div 扩展过去的容器 div 具有 x 滚动

android - Android 中的水平滚动文本

java - 从磁盘加载存储的 RSA 公钥/私钥?

android - 如何根据文本长度以编程方式更改工具栏高度和字体大小

android - 具有相同 ID 的两个 View