这件事情让我疯了。使用CoordinatorLayout和AppBarLayout变得疯狂(有时)查看动画
我已经能够得到这种行为(我想要什么):http://i.imgur.com/PGhL22k.gif
这是行为是有当我向下滚动速度非常快:http://i.imgur.com/kk7icAc.gif和http://i.imgur.com/YNPNiA6.gif
我很抱歉,但GIF大于2Mb,我无法在此处上传它们...
我想要底部的分页栏隐藏相同数量的工具栏。当慢速滚动时会非常顺利,但当快速滚动时,它会有一个非常奇怪的行为,就像您在上面提供的GIF中看到的那样。
这是布局XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:kiosk="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
layout="@layout/android_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
kiosk:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vpPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
kiosk:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<include
layout="@layout/paginator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
kiosk:layout_behavior="carl.fri.fer.views.KioskPaginator.KioskPaginatorScrollBehaviour"/>
</android.support.design.widget.CoordinatorLayout>
的 “android_toolbar” 包括如下:
<?xml version="1.0" encoding="utf-8"?>
<carl.fri.fer.views.KioskToolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:minHeight="?attr/actionBarSize"/>
与 “paginator_layout” 如下:
<?xml version="1.0" encoding="utf-8"?>
<carl.fri.fer.views.KioskPaginator.KioskPaginator
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/kpPaginator"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:animateLayoutChanges="true"
android:background="@color/primaryColor"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:id="@+id/tvCurrentPage"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:padding="10dp"
android:textColor="@color/color_pure_black"
android:textSize="17sp"/>
<LinearLayout
android:id="@+id/llMoreOptions"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:color/transparent"
android:gravity="center_vertical"
android:orientation="horizontal"/>
<TextView
android:id="@+id/tvTotalPages"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:padding="10dp"
android:textColor="@color/color_pure_black"
android:textSize="17sp"/>
</carl.fri.fer.views.KioskPaginator.KioskPaginator>
而且ScrollBehavior如下:
public class KioskPaginatorScrollBehaviour extends AppBarLayout.ScrollingViewBehavior {
public KioskPaginatorScrollBehaviour(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float depY = - dependency.getHeight();
depY -= dependency.getY();
Utils.log("dependency", "" + depY);
child.setTranslationY(depY);
return true;
}
}
在ViewPager的每一页里面都有一个片段。此片段的内容是带动态内容的RecyclerView。从数据库加载RecyclerView的所有内容,并在运行时加载图像。
有人可以告诉我为什么这种奇怪的行为发生?我该如何解决它?
预先感谢您...
编辑1:
我只是发现了怪异行为的原因! ViewPager加载当前页面和相邻页面。 RecyclerView的内容从Internet载入,一旦载入,它就会进入RecyclerView。 ViewPager首先加载当前页面,然后加载相邻页面。如果我在第二个RecyclerView加载并显示内容时向下滚动了当前页面RecyclerView(工具栏处于隐藏状态),它会重置当前页面AppBarLayout行为,因此它会重置我的自定义视图行为...我该如何解决这个问题?我想避免不加载毗邻的意见..
编辑2:
好了,它加载的ViewPager
相邻页和时也从互联网上的图像加载RecyclerView
内部时发生......这是疯了。
你能为了我们可以很容易地重现问题的地方发布的项目(例如GitHub上)? – Divers
我很抱歉,但这是一个私人项目......我认为你几乎需要所有的信息。但是,我会尝试将它抽象为另一个项目,以尝试在那里再现问题。 –
只需创建重现问题的项目。无需发布您的真实项目。 – Divers