ScrollView滚动到底部使用的scrollTo和fullScroll方法的异同

本文探讨了在Android中,如何使用ScrollView的scrollTo和fullScroll方法来实现滚动到底部的功能。虽然两者都能达到滚动目的,但fullScroll涉及到焦点变化,当参数设为FOCUS_DOWN时,会改变视图焦点。而scrollTo只是简单地移动到指定位置,不改变其他属性。因此,使用fullScroll需要注意焦点的改变,当底部元素无法获取焦点时,该方法可能无效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

scrollTo和fullScroll两个方法经常被用来做滚动到底部的操作

不过就这个功能而言 也是有不同之处的

使用fullScroll滚动到底部 使用的参数为 FOCUS_DOWN 顾名思义 和 焦点有关

fullScroll 代码如下

/**
 * <p>Handles scrolling in response to a "home/end" shortcut press. This
 * method will scroll the view to the top or bottom and give the focus
 * to the topmost/bottommost component in the new visible area. If no
 * component is a good candidate for focus, this scrollview reclaims the
 * focus.</p>
 *
 * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
 *                  to go the top of the view or
 *                  {@link android.view.View#FOCUS_DOWN} to go the bottom
 * @return true if the key event is consumed by this method, false otherwise
 */
public boolean fullScroll(int direction) {
    boolean down = direction == View.FOCUS_DOWN;
    int height = getHeight();

    mTempRect.top = 0;
    mTempRect.bottom = height;

    if (down) {
        int count = getChildCount();
        if (count > 0) {
            View view = getChildAt(count - 1);
            mTempRect.bottom = view.getBottom() + mPaddingBottom;
            mTempRect.top = mTempRect.bottom - height;
        }
    }

    return scrollAndFocus(direction, mTempRect.top, mTempRect.bottom);
}

/**
 * <p>Scrolls the view to make the area defined by <code>top</code> and
 * <code>bottom</code> visible. This method attempts to give the focus
 * to a component visible in this area. If no component can be focused in
 * the new visible area, the focus is reclaimed by this ScrollView.</p>
 *
 * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
 *                  to go upward, {@link android.view.View#FOCUS_DOWN} to downward
 * @param top       the top offset of the new area to be made visible
 * @param bottom    the bottom offset of the new area to be made visible
 * @return true if the key event is consumed by this method, false otherwise
 */
private boolean scrollAndFocus(int direction, int top, int bottom) {
    boolean handled = true;

    int height = getHeight();
    int containerTop = getScrollY();
    int containerBottom = containerTop + height;
    boolean up = direction == View.FOCUS_UP;

    View newFocused = findFocusableViewInBounds(up, top, bottom);
    if (newFocused == null) {
        newFocused = this;
    }

    if (top >= containerTop && bottom <= containerBottom) {
        handled = false;
    } else {
        int delta = up ? (top - containerTop) : (bottom - containerBottom);
        doScrollY(delta);
    }

    if (newFocused != findFocus()) newFocused.requestFocus(direction);

    return handled;
}

可以看到 fullScroll将视图滚动到底部 同时焦点作出了改变

scrollTo方法 只执行移动至某位置 不对其他属性作出修改

因此 使用 fullScroll 来执行滚动到底部时 需注意 此时你的UI焦点已经发生变化 且 底部UI无法获得焦点时 此方法无效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值