Code example for ScrollView

Methods: getHeight, getLocationOnScreen, getVerticalFadingEdgeLength

0
        // now we are at the first button off screen 
        int buttonLoc[] = {0, 0};
        firstButtonOffScreen.getLocationOnScreen(buttonLoc);
        int buttonBottom = buttonLoc[1] + firstButtonOffScreen.getHeight();
 
        int verticalFadingEdgeLength = mScrollView
                .getVerticalFadingEdgeLength();
        assertEquals("bottom of button should be verticalFadingEdgeLength "
                + "above the bottom of the screen", 
                buttonBottom, mScreenBottom - verticalFadingEdgeLength);
    } 
 
    // there should be no offset for vertical fading edge 
    // if the item is the last one on screen 
    @LargeTest 
    public void testArrowScrollDownToBottomElementOnScreen() { 
 
        int numGroups = getActivity().getNumButtons();
        Button lastButton = getActivity().getButton(numGroups - 1);
 
        assertEquals("button needs to be at the very bottom of the layout for "
                + "this test to work", 
                mLinearLayout.getHeight(), lastButton.getBottom());
 
        // move down to last button 
        for (int i = 0; i < numGroups; i++) {
            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
        } 
        getInstrumentation().waitForIdleSync();
        assertTrue("last button should have focus", lastButton.hasFocus());
 
        int buttonLoc[] = {0, 0};
        lastButton.getLocationOnScreen(buttonLoc);
        int buttonBottom = buttonLoc[1] + lastButton.getHeight();
        assertEquals("button should be at very bottom of screen",
                mScreenBottom, buttonBottom);
    } 
 
    @LargeTest 
    public void testArrowScrollUpOffScreenVerticalFadingEdge() { 
        // get to bottom button 
        int numGroups = goToBottomButton();
 
        // go up to first off screen button 
        int offScreenIndex = findFirstButtonOffScreenBottom2Top();
        Button offScreenButton = getActivity().getButton(offScreenIndex);
        int clicksToOffScreenIndex = numGroups - offScreenIndex - 1;
        for (int i = 0; i < clicksToOffScreenIndex; i++) {
            sendKeys(KeyEvent.KEYCODE_DPAD_UP);
        } 
        getInstrumentation().waitForIdleSync();
        assertTrue("we want to be at offScreenButton", offScreenButton.hasFocus());
 
        // top should take into account fading edge 
        int buttonLoc[] = {0, 0};
        offScreenButton.getLocationOnScreen(buttonLoc);
        assertEquals("top should take into account fading edge",
            mScreenTop + mScrollView.getVerticalFadingEdgeLength(), buttonLoc[1]);
    } 
 
 
    @LargeTest 
    public void testArrowScrollUpToTopElementOnScreen() { 
        // get to bottom button 
        int numButtons = goToBottomButton();
 
        // go back to the top 
        for (int i = 0; i < numButtons; i++) {
            sendKeys(KeyEvent.KEYCODE_DPAD_UP);
        } 
        getInstrumentation().waitForIdleSync();
 
        Button topButton = getActivity().getButton(0);
        assertTrue("should be back at top button", topButton.hasFocus());
 
 
        int buttonLoc[] = {0, 0};
        topButton.getLocationOnScreen(buttonLoc);
        assertEquals("top of top button should be at top of screen; no need to take"
                + " into account vertical fading edge.", 
                mScreenTop, buttonLoc[1]);
    } 
 
    private int goToBottomButton() { 
        int numButtons = getActivity().getNumButtons();
        Button lastButton = getActivity().getButton(numButtons - 1);
 
        for (int i = 0; i < numButtons; i++) {
          sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
        } 
        getInstrumentation().waitForIdleSync();
        assertTrue("we want to be at the last button", lastButton.hasFocus());
        return numButtons;
    } 
 
    // search from top to bottom for the first button off screen 
    private int findFirstButtonOffScreenTop2Bottom() { 
        int origin[] = {0, 0};
        mScrollView.getLocationOnScreen(origin);
        int screenHeight = mScrollView.getHeight();
 
        for (int i = 0; i < getActivity().getNumButtons(); i++) {
 
            int buttonLoc[] = {0, 0};
            Button button = getActivity().getButton(i);
Contextual code suggestions in your IDE  Get Codota for Java