2

這件事情讓我瘋了。使用CoordinatorLayout和AppBarLayout變得瘋狂(有時)查看動畫

我已經能夠得到這種行爲(我想要什麼):http://i.imgur.com/PGhL22k.gif

這是行爲是有當我向下滾動速度非常快:http://i.imgur.com/kk7icAc.gifhttp://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內部時發生......這是瘋了。

+0

你能爲了我們可以很容易地重現問題的地方發佈的項目(例如GitHub上)? – Divers

+0

我很抱歉,但這是一個私人項目......我認爲你幾乎需要所有的信息。但是,我會嘗試將它抽象爲另一個項目,以嘗試在那裏再現問題。 –

+0

只需創建重現問題的項目。無需發佈您的真實項目。 – Divers

回答

3

因此,最終解決了您的問題 - 使您的自定義行爲延伸CoordinatorLayout.Behavior而不是ScrollingViewBehavior,它將按預期工作。只需將該值設爲您的視圖的translationY,這是相反的AppBarLayout Y:

public class KioskPaginatorScrollBehaviour extends CoordinatorLayout.Behavior<View> { 

    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.getY(); 
     child.setTranslationY(depY); 
     return true; 
    } 
} 
+0

你是我的英雄!我會做更多的測試,只要我確信這是最終修復,我會以+200的聲望獎勵你)。我現在將對「AppBarLayout.ScrollingBehavior」和「CoordinatorLayout.Behaviour」之間的區別進行一些研究,但同時你知道嗎?非常感謝你!!! –

+0

好吧,'AppBarLayout.ScrollingViewBehavior'擴展了'CoordinatorLayout.Behavior'本身,並有一些嵌套滾動的邏輯。按照指南中的建議,當您想創建自定義行爲時,您應該擴展'CoordinatorLayout.Behavior' :) – rom4ek

+0

非常完美,非常感謝。在閱讀如何創建自定義行爲時,我應該錯過一些重要的東西...... –