使用Espresso滚动新的ListActivity

时间:2015-06-23 14:10:26

标签: android android-espresso

如何点击我无法看到lit7.txt的项目?

我无法使用Espresso滚动MyListActivity并点击特定项目。我想点击lit7.txt项目,但我无法在屏幕上看到它。我需要向下滚动。但是如何?

screen image with MyListActivity(DirectoryViewer) and lit7.txt

我可以点击"浏览..."

Espresso.onView(Matchers.allOf(ViewMatchers.withText("Browse..."))).perform(ViewActions.click());

我可以点击histo2.txt

Espresso.onView(Matchers.allOf(ViewMatchers.withText("histo2.txt"))).perform(ViewActions.click());

但是如何点击我无法看到lit7.txt的项目?

所以,我加载了ListActivity(DirectoryViewer)

protected void onLoad(){
        Intent intent = new Intent(this, DirectoryViewer.class);
        intent.putExtra(DirectoryViewer.ASSET_DIRECTORY_LOCATION, "books");
        startActivityForResult(intent, 1);}

DirectoryViewer是:

class DirectoryViewer extends ListActivity
...
    void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String dirName = getIntent().getStringExtra(ASSET_DIRECTORY_LOCATION);
        List<ListItem> items = new ArrayList<ListItem>();
        items.add(new BrowseListItem(getString(R.string.my_browse)));//Browse...
        items.add(new HeaderItem(getString(R.string.myItems)));//ITEMS
        AssetManager am = getResources().getAssets();//files
        try {
            String[] topLevel = am.list(dirName);
            for (String folder : topLevel) {
            ...
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

我建议查看onData method,它允许您与ListView中的信息进行交互。

答案 1 :(得分:0)

这将滚动列表视图,直到第20个项目可见。

 onData(hasEntry(equalTo(ListViewSample.ROW_TEXT),is("List item: 20")))
        .check(matches(isCompletelyDisplayed()));