CollapsingToolbarLayout不承认滚动一扔 [英] CollapsingToolbarLayout doesn't recognize scroll fling

查看:1036
本文介绍了CollapsingToolbarLayout不承认滚动一扔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的 Col​​lapsingToolbarLayout 和它的作品就像一个魅力。我的问题是,如果我尝试使用一扔滚动的 nestedscrollview ,当我松开我的手指刚刚停止。正常滚动的作品像它应该。

我的活动code是 不变=>自动产生的空活动。 (我刚刚点击创建机器人工作室新的空活动,并编辑XML还)。

我在这里阅读,滚动手势的ImageView的本身有Bug,但是没有,那滚动本身有错误:<一href="https://$c$c.google.com/p/android/issues/detail?id=176673">https://$c$c.google.com/p/android/issues/detail?id=176673

我试图激活的平滑滚动通过Java code。好像如果我滚动远远不够的ImageView的不可见了,一扔手势则是公认的。

TLDR:为什么一扔手势不是只要ImageView的是可见的工作? 我的XML code是这样的:

 &LT; android.support.design.widget.CoordinatorLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fitsSystemWindows =真正的&GT;

    &LT; android.support.design.widget.AppBarLayout
        机器人:ID =@ + ID / profile_app_bar_layout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar
        机器人:fitsSystemWindows =真正的&GT;

        &LT; android.support.design.widget.CollapsingToolbarLayout
            机器人:ID =@ + ID / profile_collapsing_toolbar_layout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            应用程序:layout_scrollFlags =滚动| exitUntilCollapsed
            应用程序:contentScrim =?ATTR / colorPrimary
            应用程序:expandedTitleMarginStart =48dp
            应用程序:expandedTitleMarginEnd =64dp
            机器人:fitsSystemWindows =真正的&GT;

            &LT; ImageView的
                机器人:ID =@ + ID /图像
                机器人:layout_width =match_parent
                机器人:layout_height =420dp
                机器人:scaleType =centerCrop
                机器人:fitsSystemWindows =真
                机器人:SRC =@可绘制/ headerbg
                机器人:=了maxHeight192dp

                应用程序:layout_collapseMode =视差/&GT;

            &LT; android.support.v7.widget.Toolbar
                机器人:ID =@ + ID /工具栏
                机器人:layout_width =match_parent
                机器人:layout_height =?ATTR / actionBarSize
                应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light
                应用程序:layout_collapseMode =针/&GT;

        &LT; /android.support.design.widget.CollapsingToolbarLayout>

    &LT; /android.support.design.widget.AppBarLayout>

    &LT; android.support.design.widget.FloatingActionButton
        机器人:ID =@ + ID /晶圆厂
        应用程序:layout_anchor =@ ID / profile_app_bar_layout
        应用程序:layout_anchorGravity =底部|右|结束
        机器人:layout_height =@扪/ fab_size_normal
        机器人:layout_width =@扪/ fab_size_normal
        应用程序:海拔=2DP
        应用程序:pressedTranslationZ =12dp
        机器人:layout_marginRight =8DP
        机器人:layout_marginEnd =8DP/&GT;

    &LT; android.support.v4.widget.NestedScrollView
        机器人:ID =@ + ID / profile_content_scroll
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:clipToPadding =假
        应用程序:layout_behavior =@字符串/ appbar_scrolling_view_behavior
        机器人:layout_gravity =fill_vertical
        机器人:=了minHeight192dp
        机器人:overScrollMode =ifContentScrolls
        &GT;

        &LT; RelativeLayout的
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT&GT;

            &LT;的TextView
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=@字符串/ LoremIpsum/&GT;

        &LT; / RelativeLayout的&GT;

    &LT; /android.support.v4.widget.NestedScrollView>

&LT; /android.support.design.widget.CoordinatorLayout>
 

解决方案

从答:

<一个href="http://stackoverflow.com/a/32454407/2281718">http://stackoverflow.com/a/32454407/2281718

这解决了这个问题对我来说。创建一个自定义的 AppBarLayout.Behavior 是这样的:

 公开最后一类FlingBehavior扩展AppBarLayout.Behavior {
    私有静态最终诠释TOP_CHILD_FLING_THRESHOLD = 3;
    私人布尔isPositive;

    公共FlingBehavior(){
    }

    公共FlingBehavior(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    @覆盖
    公共布尔onNestedFling(CoordinatorLayout coordinatorLayout,AppBarLayout孩子,查看目标,浮velocityX,浮velocityY,布尔消耗){
        如果(velocityY大于0&安培;&安培;!isPositive || velocityY&℃,安培;&安培; isPositive){
            velocityY = velocityY * -1;
        }
        如果(目标的instanceof RecyclerView&安培;&安培; velocityY℃,){
            最后RecyclerView recyclerView =(RecyclerView)的目标;
            最终视图则firstChild = recyclerView.getChildAt(0);
            最终诠释childAdapterPosition = recyclerView.getChildAdapterPosition(则firstChild);
            消费= childAdapterPosition&GT; TOP_CHILD_FLING_THRESHOLD;
        }
        返回super.onNestedFling(coordinatorLayout,儿童,目标,velocityX,velocityY,消耗);
    }

    @覆盖
    公共无效onNested preScroll(CoordinatorLayout coordinatorLayout,AppBarLayout孩子,查看目标,INT DX,DY INT,INT []消耗){
        super.onNested preScroll(coordinatorLayout,儿童,目标,DX,DY,消耗);
        isPositive = DY&GT; 0;
    }
}
 

和它添加到 AppBarLayout 是这样的:

 &LT; android.support.design.widget.AppBarLayout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        ...
        应用程序:layout_behavior =com.example.test.FlingBehavior&GT;
 

I have created a simple CollapsingToolbarLayout and it works like a charm. My problem is, that if I try to use a fling scroll on the nestedscrollview, it just stops when I release my finger. Normal scrolling works like it should.

My activities code is unchanged => auto generated empty activity. (I just clicked on create new empty activity in android studio and edited the XML yet).

I read here, that scroll gestures on the imageview itself are buggy, but not, that the scrolling itself is buggy: https://code.google.com/p/android/issues/detail?id=176673

I tried activating "smooth scrolling" through java code. It seems like if I scroll far enough that the imageview isn't visible anymore, fling gestures then are recognized.

TLDR: Why does the fling gesture not work as long as the imageview is visible ? My XML Code looks like this:

    <android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/profile_collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            android:fitsSystemWindows="true">

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="420dp"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                android:src="@drawable/headerbg"
                android:maxHeight="192dp"

                app:layout_collapseMode="parallax"/>

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

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        app:layout_anchor="@id/profile_app_bar_layout"
        app:layout_anchorGravity="bottom|right|end"
        android:layout_height="@dimen/fab_size_normal"
        android:layout_width="@dimen/fab_size_normal"
        app:elevation="2dp"
        app:pressedTranslationZ="12dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"/>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/profile_content_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_gravity="fill_vertical"
        android:minHeight="192dp"
        android:overScrollMode="ifContentScrolls"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/LoremIpsum"/>

        </RelativeLayout>

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

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

解决方案

Answer from:

http://stackoverflow.com/a/32454407/2281718

which solved this problem for me. Create a custom AppBarLayout.Behavior like this:

public final class FlingBehavior extends AppBarLayout.Behavior {
    private static final int TOP_CHILD_FLING_THRESHOLD = 3;
    private boolean isPositive;

    public FlingBehavior() {
    }

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

    @Override
    public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) {
        if (velocityY > 0 && !isPositive || velocityY < 0 && isPositive) {
            velocityY = velocityY * -1;
        }
        if (target instanceof RecyclerView && velocityY < 0) {
            final RecyclerView recyclerView = (RecyclerView) target;
            final View firstChild = recyclerView.getChildAt(0);
            final int childAdapterPosition = recyclerView.getChildAdapterPosition(firstChild);
            consumed = childAdapterPosition > TOP_CHILD_FLING_THRESHOLD;
        }
        return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
        isPositive = dy > 0;
    }
}

and add it to the AppBarLayout like this:

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...
        app:layout_behavior="com.example.test.FlingBehavior">

这篇关于CollapsingToolbarLayout不承认滚动一扔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆