【问题标题】:Smooth ImageView scroll in CoordinatorLayout在 CoordinatorLayout 中平滑 ImageView 滚动
【发布时间】:2015-12-30 11:36:09
【问题描述】:

我的应用程序的 CoordinatorLayout 出现平滑滚动问题。

我试图做到这一点: http://wstaw.org/m/2015/10/02/google-scroll.gif

但我最好的结果是: http://wstaw.org/m/2015/10/02/my-scroll.gif

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

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

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="@dimen/detail_image_height"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            app:layout_scrollFlags="scroll|exitUntilCollapsed" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:background="?attr/colorPrimary"
            android:minHeight="80dp">

            (...)

        </RelativeLayout>

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            (...)

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

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

我做错了什么?提前致谢。

【问题讨论】:

标签: android android-design-library android-coordinatorlayout


【解决方案1】:

我无法完全解决此问题,但我确实找到了有助于向上滚动的内容。它基于this answer 在一个关于使用 CoordinatorLayout 的 SO 线程。首先,创建一个扩展 AppBarLayout.Behavior 的类。

/**
 * This "fixes" the weird scroll behavior with CoordinatorLayouts with NestedScrollViews when scrolling up.
 * This is based on https://stackoverflow.com/questions/30923889/flinging-with-recyclerview-appbarlayout
 */
@SuppressWarnings("unused")
public class CoordinatorFlingBehavior extends AppBarLayout.Behavior {
    private static final String TAG = "CoordinatorFling";

    public CoordinatorFlingBehavior() {
    }

    public CoordinatorFlingBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) {
        // Passing false for consumed will make the AppBarLayout fling everything and pull down the expandable stuff
        if (target instanceof NestedScrollView && velocityY < 0) {
            final NestedScrollView scrollView = (NestedScrollView) target;
            int scrollY = scrollView.getScrollY();

            // Note the ! in front
            consumed = !(scrollY < target.getContext().getResources().getDimensionPixelSize(R.dimen.flingThreshold) // if below threshold, fling
                || isScrollingUpFast(scrollY, velocityY)); // Or if moving quickly, fling

            Log.v(TAG, "onNestedFling: scrollY = " + scrollY + ", velocityY = " + velocityY + ", flinging = " + !consumed);
        }
        return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    }

    /**
     * This uses the log of the velocity because constants make it too easy to uncouple the CoordinatorLayout - the AppBarLayout and the NestedScrollView - when scrollPosition is small.
     *
     * @param scrollPosition - of the NestedScrollView target
     * @param velocityY      - Y velocity. Should be negative, because scrolling up is negative. However, a positive value won't crash this method.
     * @return true if scrolling up fast
     */
    private boolean isScrollingUpFast(int scrollPosition, float velocityY) {
        float positiveVelocityY = Math.abs(velocityY);

        double calculation = scrollPosition * Math.log(positiveVelocityY);

        return positiveVelocityY > calculation;
    }

}

然后,将以下行添加到您的 AppBarLayout 的 xml 块中(将公司名称和包替换为您使用的任何内容):

    app:layout_behavior="com.companyname.packages.CoordinatorFlingBehavior"

【讨论】:

  • 你在 R.dimen.flingThreshold 中使用了什么值?
  • 100dp
猜你喜欢
  • 2016-12-18
  • 2016-11-02
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多