【问题标题】:How to programmatically scroll NestedScrollView within a CoordinatorLayout?如何以编程方式在 CoordinatorLayout 中滚动 NestedScrollView?
【发布时间】:2016-02-25 13:38:22
【问题描述】:

我有 CoordinatorLayout 和里面的以下项目:

  1. 带有可折叠工具栏的AppBarLayout;
  2. 带有一些内容的 NestedScrollView

我想以编程方式向上滚动我的 NestedScrollView,直到折叠可折叠工具栏。

我试过这样的代码:

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if (behavior != null) {
    behavior.onNestedPreScroll(coordinatorLayout, appBarLayout, nestedScrollView, 0, 1000, new int[2]);
}

但它只是向上滚动并折叠 AppBar 布局本身,而 NestedScrollView 保持在原位。

那么,问题是如何向上滚动 NestedScrollView 并使 Collapsable Toolbar 折叠?

我知道这个问题与 Coordinator Layout 的行为有关,但我不知道遗漏了什么。

这是确切的布局:

<android.support.design.widget.CoordinatorLayout
android:id="@+id/event_coordinator_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <!-- some content -->

        <android.support.v7.widget.Toolbar
            android:id="@+id/quick_return_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nested_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!-- some content -->

</android.support.v4.widget.NestedScrollView>

【问题讨论】:

    标签: android android-layout material-design android-coordinatorlayout


    【解决方案1】:

    CollapsingToolbarLayout 折叠基于AppBarLayout 偏移其子项的顶部和底部。您正在通过调用行为嵌套的预滚动方法正确偏移它。 NestedScrollView 也需要滚动相同的量:

            int targetScroll = mNestedScrollView.getScrollY() + 1000;
            mNestedScrollView.scrollTo(0,targetScroll);
            mNestedScrollView.setSmoothScrollingEnabled(true);
            ViewCompat.setNestedScrollingEnabled(mNestedScrollView, false);
            final int currentScrollY = mNestedScrollView.getScrollY();
            ViewCompat.postOnAnimationDelayed(mNestedScrollView, new Runnable() {
                int currentY = currentScrollY;
                @Override
                public void run() {
                    if(currentScrollY == mNestedScrollView.getScrollY()){
                        ViewCompat.setNestedScrollingEnabled(mNestedScrollView, true);
                        return;
                    }
                    currentY = mNestedScrollView.getScrollY();
                    ViewCompat.postOnAnimation(mNestedScrollView, this);
                }
            }, 10);
    

    【讨论】:

    猜你喜欢
    • 2018-06-01
    • 2015-09-09
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多