CollapsingToolbarLayout 无法识别滚动条 [英] CollapsingToolbarLayout doesn't recognize scroll fling
问题描述
我创建了一个简单的CollapsingToolbarLayout,它的作用就像一个魅力.我的问题是,如果我尝试在 nestedscrollview 上使用滑动滚动,它会在我松开手指时停止.正常滚动的效果应该是这样.
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.
我的活动代码是未更改 => 自动 生成的空活动.(我刚刚在 android studio 中点击了 create new empty Activity 并编辑了 XML).
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: see here.
我尝试通过 java 代码激活平滑滚动".似乎如果我滚动到足够远的图像视图不再可见,然后就会识别出甩动手势.
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:为什么只要图像视图可见,甩动手势就不起作用?我的 XML 代码如下所示:
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>
推荐答案
我在使用 CollapsingToolbarLayout 和 ImageView 和 NestedScrollView 时遇到了完全相同的问题>.松开手指时滑动滚动停止.
I had exactly the same issue with CollapsingToolbarLayout with ImageView inside and NestedScrollView. The fling scroll stops when finger is released.
但是,我注意到了一些奇怪的事情.如果您从带有 OnClickListener(例如 Button)的视图开始用手指滚动,则可以完美地进行滚动.
However, I've noticed something strange. If you start scrolling with your finger from a view with OnClickListener (e.g. Button), the fling scrolling works perfectly.
因此我用一个奇怪的解决方案修复了它.在 NestedScrollView 的直接子级上设置 OnClickListener(什么都不做).然后它完美地工作!
Thus I fixed it with a weird solution. Set OnClickListener (that does nothing) on the direct child of NestedScrollView. Then it works perfectly!
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Page Content -->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
给直接子(LinearLayout)一个id并在Activity中设置OnClickListener
Give the direct child (LinearLayout) an id and set OnClickListener in Activity
ViewGroup mContentContainer = (ViewGroup) findViewById(R.id.content_container);
mContentContainer.setOnClickListener(this);
@Override
public void onClick(View view) {
int viewId = view.getId();
}
注意事项:
使用支持设计库 25.0.1 测试
Tested using Support Design Library 25.0.1
CollapsingToolbarLayout with scrollFlags="scroll|enterAlwaysCollapsed"
这篇关于CollapsingToolbarLayout 无法识别滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!