22

Tôi có hoạt động với một số CoordinatorLayout, AppBarLayout, CollapsingToolbarLayoutToolbar. Vì vậy, về cơ bản, một chế độ xem thu gọn khi cuộn một số RecyclerView.Hiển thị chế độ xem khi thanh công cụ thu hẹp

Điều tôi cần làm là hiển thị chế độ xem tùy chỉnh khi chế độ xem bố cục được mở rộng bị ẩn do thu gọn.

Đây là cách bố trí của tôi:

<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/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="192dp" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      app:contentScrim="#2196F3" 
      app:expandedTitleMarginBottom="32dp" 
      app:expandedTitleMarginEnd="64dp" 
      app:expandedTitleMarginStart="48dp" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <include 
       android:id="@+id/header" 
       layout="@layout/header_big_first_screen" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:fitsSystemWindows="true" 
       android:scaleType="centerCrop" 
       app:layout_collapseMode="parallax"/> 

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

       <TextView 

        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="Hello!"/> 


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


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

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

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/categories_recyclerview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 


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

Cuối cùng, khi thanh công cụ được mở rộng quan điểm nạp với nguyên tố này được hiển thị. Khi nó sụp đổ thì không. Khi nó biến mất, TextView bên trong Toolbar sẽ được hiển thị. Hiện tại nó hiển thị mọi lúc.

Tôi đã tìm kiếm các sự kiện của CollapsingToolbarLayout để thêm người nghe khi thay đổi kích thước để tôi có thể kiểm tra xem đó có nhỏ hơn giá trị và hiển thị chế độ xem đó hay không.

Điều này có thể khá khó giải thích nhưng tôi tin rằng tôi đã tự làm rõ. Tôi đã googling xung quanh và không thể tìm thấy bất cứ ai cố gắng để làm như vậy.

+0

Bạn có thể sử dụng contentScrim không? – tachyonflux

+0

@karaokyo setContentScrim và các biến thể của nó chỉ dành cho drawables và màu sắc, trừ khi tôi đang thiếu thứ gì đó. https://developer.android.com/reference/android/support/design/widget/CollapsingToolbarLayout.html –

Trả lời

50

Nhìn vào nguồn CollapsingToolbarLayout, hoạt ảnh quét nội dung được kích hoạt qua OnOffsetChangedListener trên AppBarLayout. Vì vậy, bạn có thể thêm một số khác để kích hoạt hoạt ảnh alpha trên chế độ xem văn bản của mình:

mListener = new AppBarLayout.OnOffsetChangedListener() { 
    @Override 
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { 
     if(collapsingToolbar.getHeight() + verticalOffset < 2 * ViewCompat.getMinimumHeight(collapsingToolbar)) { 
      hello.animate().alpha(1).setDuration(600); 
     } else { 
      hello.animate().alpha(0).setDuration(600); 
     } 
    } 
}; 

appBar.addOnOffsetChangedListener(mListener); 
+0

Hoàn toàn hoạt động! Cảm ơn rất nhiều! Tâm trí câu hỏi: Tôi đang nhìn vào việc thực hiện của 'CollapsibleToolbarLayout' nhưng làm thế nào bạn đã nhận được giá trị' (2 * ViewCompat.getMinimumHeight (collapsingToolbar)) '? Cảm ơn một lần nữa –

+2

Đó là cùng một kích hoạt mà nội dung scrim sử dụng – tachyonflux

+0

Cảm ơn các giải pháp @karaokyo Công trình như một sự quyến rũ! Tuy nhiên, trong khi thiết bị di động của bạn không sao, nếu các trường hợp khác của bạn không được kích hoạt trên máy tính bảng. Bất kỳ ý tưởng tại sao là điều đó? Cảm ơn bạn. – Suleiman19

Các vấn đề liên quan