首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在CoordinatorLayout中平滑ImageView滚动

在CoordinatorLayout中平滑ImageView滚动
EN

Stack Overflow用户
提问于 2015-10-02 17:18:56
回答 1查看 2K关注 0票数 3

我有一个问题,在我的应用程序中的CoordinatorLayout平滑滚动。

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

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

代码语言:javascript
运行
复制
<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>

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

EN

Stack Overflow用户

发布于 2016-01-01 02:56:51

我不能完全修复这个行为,但我确实找到了一些有助于向上滚动的东西。它是基于一个关于CoordinatorLayout的SO线程中的this answer。首先,创建一个扩展AppBarLayout.Behavior的类。

代码语言:javascript
运行
复制
/**
 * 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块中(将companyname和packages替换为您使用的任何内容):

代码语言:javascript
运行
复制
    app:layout_behavior="com.companyname.packages.CoordinatorFlingBehavior"
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32904110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档