NestedScrollView 的 smoothScrollTo() 行为怪异

NestedScrollView's smoothScrollTo() behaves weird

我想要实现的是在单击 tab1(等等)时滚动到 scroll_position_1,如 this。我根本不明白发生了什么。下面是我的布局结构。

<?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>

这是我在点击选项卡时滚动但不起作用的方式:

mNestedScroller.smoothScrollTo(0, scroll_position_1_text_view.getY());    

而将其修改为:

mNestedScroller.scrollTo(0, scroll_position_1_text_view.getY());

工作没有问题!请帮忙。谢谢!

您可以使用 ListView 而不是 ConstraintLayout 并将文本视图添加到列表中。然后,您可以简单地调用 listView.smoothScrollToPosition(index) 而不必担心 x 和 y 坐标。

1) 如下所示编辑布局。

2) 通过代码将项目添加到您的 ListView。

3) 单击选项卡后,调用 listView.smoothScrollToPosition(index_of_text_view),您的列表视图将滚动到该位置。

<?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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

      <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

          <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>

          <ListView
           android:id="@+id/list_view"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"/>
       </LinearLayout
</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>

单击选项卡时,只需获取所需视图相对于根视图的 y 位置,然后滚动到该位置。

public class MainActivity extends AppCompatActivity {

  private NestedScrollView nestedScrollView;
  private CoordinatorLayout coordinatorLayout;
  TextView textView;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    nestedScrollView = findViewById(R.id.nsv);
    coordinatorLayout = findViewById(R.id.cl);
    textView = findViewById(R.id.scroll_position_1);

    TabLayout tabLayout = findViewById(R.id.tl);
    tabLayout.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
      @Override
      public void onTabSelected(TabLayout.Tab tab) {
        if(tab.getPosition() == 1/*position of your desired tab*/){
          scrollToView(textView);
        }
      }

      @Override
      public void onTabUnselected(TabLayout.Tab tab) {

      }

      @Override
      public void onTabReselected(TabLayout.Tab tab) {

      }
    });

  }

  /*
   * Used to scroll to the given view.
   *
   * @param view View to which we need to scroll.
   */
  private void scrollToView(View view) {
    // Get deepChild Offset
    int position = getRelativeTop(view);
    final int finalPosition = position;
    nestedScrollView.fling(0);
    nestedScrollView.smoothScrollTo(0, finalPosition);
  }

  private int getRelativeTop(View myView) {
    Rect offsetViewBounds = new Rect();
//returns the visible bounds
    myView.getDrawingRect(offsetViewBounds);
// calculates the relative coordinates to the parent
    coordinatorLayout.offsetDescendantRectToMyCoords(myView, offsetViewBounds);

    int relativeTop = offsetViewBounds.top;
    int relativeLeft = offsetViewBounds.left;
    return relativeTop;
  }

}

在 CoordinatorLayout 中以编程方式滚动 NestedScrollView 时似乎存在错误。这解决了我的问题:

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.fling(0);
nestedScrollView.smoothScrollTo(0, 0);