首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式滚动到屏幕的末尾

以编程方式滚动到屏幕的末尾
EN

Stack Overflow用户
提问于 2017-03-18 14:52:08
回答 2查看 24.3K关注 0票数 15

我已经在我的应用程序中实现了TapTargetView库。

在传递某个元素之后,我需要专注于屏幕外的下一个视图:

代码语言:javascript
复制
@Override
public void onSequenceStep(TapTarget lastTarget) {
  if (lastTarget.id() == 7) {
     flavorContainer.setFocusableInTouchMode(true);
     flavorContainer.requestFocus();
  }
}

在我将广告单元添加到屏幕底部之前,一切都很好。因此,现在必要的元素显示在广告后面。

方法requestFocus()滚动布局只显示到必要的视图,但不显示到屏幕的末尾。

我需要一种将屏幕内容滚动到非常末端的的方法,而不仅仅是在屏幕上可以看到所需的视图。有可能吗?

布局结构

代码语言:javascript
复制
<android.support.design.widget.CoordinatorLayout>
<LinearLayout>
<android.support.v4.widget.NestedScrollView> 
<LinearLayout> 
<android.support.v7.widget.CardView> 
<LinearLayout>

</LinearLayout> 
</android.support.v7.widget.CardView> 
</LinearLayout> 
</android.support.v4.widget.NestedScrollView> 
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
EN

回答 2

Stack Overflow用户

发布于 2017-03-18 16:51:02

你有两个可能的解决方案,正反两方面。

First

fullScroll(int)上使用NestedScrollView方法。必须在使用此方法之前绘制NestedScrollView,并且焦点将丢失在以前获得该方法的View上。

代码语言:javascript
复制
nestedScrollView.post(new Runnable() {
    @Override
    public void run() {
        nestedScrollView.fullScroll(View.FOCUS_DOWN);
    }
});

第二

使用scrollBy(int,int)/smoothScrollBy(int,int)方法。它需要更多的代码,但是您不会失去当前的焦点:

代码语言:javascript
复制
nestedScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        final int scrollViewHeight = nestedScrollView.getHeight();
        if (scrollViewHeight > 0) {
            nestedScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            final View lastView = nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1);
            final int lastViewBottom = lastView.getBottom() + nestedScrollView.getPaddingBottom();
            final int deltaScrollY = lastViewBottom - scrollViewHeight - nestedScrollView.getScrollY();
            /* If you want to see the scroll animation, call this. */
            nestedScrollView.smoothScrollBy(0, deltaScrollY);
            /* If you don't want, call this. */
            nestedScrollView.scrollBy(0, deltaScrollY);
        }
    }
});
票数 60
EN

Stack Overflow用户

发布于 2020-10-26 17:20:50

对我来说这是最有效的。它滚动到底部。

代码语言:javascript
复制
scrollView.smoothScrollTo(0, scrollView.getChildAt(0).height)
// scrollview has always only one child
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42875861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档