NestedScrollView 的 smoothScrollTo() 表现怪异 [英] NestedScrollView's smoothScrollTo() behaves weird
本文介绍了NestedScrollView 的 smoothScrollTo() 表现怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要实现的是当 tab1
(等等)被点击时滚动到 scroll_position_1
就像 这个.我根本不明白发生了什么.下面是我的布局结构.
What I am trying to achieve is to scroll to scroll_position_1
when tab1
(and so on) is clicked like this. I don't understand what's happening at all. Below is the structure of my layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/scroll_position_1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/scroll_position_2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|snap">
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<android.support.design.widget.TabItem
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</android.support.design.widget.TabLayout>
</android.support.design.widget.CoordinatorLayout>
这是我在点击标签时滚动但不起作用的方式:
This is how I scroll upon tab click but does not work:
mNestedScroller.smoothScrollTo(0, scroll_position_1_text_view.getY());
而将其修改为:
mNestedScroller.scrollTo(0, scroll_position_1_text_view.getY());
工作没有问题!请帮忙.谢谢!
works without problem! Please help. Thanks!
推荐答案
在 CoordinatorLayout 中以编程方式滚动 NestedScrollView 时似乎存在错误.这解决了我的问题:
Looks like there is a bug when programmatically scrolling NestedScrollView within CoordinatorLayout. This solved my problem:
private void scrollToView(final View view) {
mScroller.scrollBy(0, 1);
mScroller.smoothScrollTo(0, view.getTop());
}
或者为了更好的控制:
private void scrollToView(final View view) {
mScroller.scrollBy(0, 1);
ObjectAnimator.ofInt(mScroller, "scrollY", view.getTop()).setDuration(700).start();
}
这篇关于NestedScrollView 的 smoothScrollTo() 表现怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文