我已经在我的应用程序中实现了TapTargetView库。
在传递某个元素之后,我需要专注于屏幕外的下一个视图:
@Override
public void onSequenceStep(TapTarget lastTarget) {
if (lastTarget.id() == 7) {
flavorContainer.setFocusableInTouchMode(true);
flavorContainer.requestFocus();
}
}在我将广告单元添加到屏幕底部之前,一切都很好。因此,现在必要的元素显示在广告后面。

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

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

布局结构
<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>发布于 2017-03-18 16:51:02
你有两个可能的解决方案,正反两方面。
First
在fullScroll(int)上使用NestedScrollView方法。必须在使用此方法之前绘制NestedScrollView,并且焦点将丢失在以前获得该方法的View上。
nestedScrollView.post(new Runnable() {
@Override
public void run() {
nestedScrollView.fullScroll(View.FOCUS_DOWN);
}
});第二
使用scrollBy(int,int)/smoothScrollBy(int,int)方法。它需要更多的代码,但是您不会失去当前的焦点:
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);
}
}
});发布于 2020-10-26 17:20:50
对我来说这是最有效的。它滚动到底部。
scrollView.smoothScrollTo(0, scrollView.getChildAt(0).height)
// scrollview has always only one childhttps://stackoverflow.com/questions/42875861
复制相似问题