採用 MotionLayout 的輪轉介面

Carousel 是一個動作輔助物件,用於建構自訂輪轉介面檢視畫面,並顯示使用者可略過的元素清單。與其他實作這類檢視畫面的方式相比,此輔助程式可讓您運用 MotionLayout,為 Carousel 快速建立複雜的動作和維度變更。

Carousel 小工具支援包含開始和結束的清單,以及圓形環繞清單。

搭配 MotionLayout 使用輪轉介面的運作方式

假設您要建構水平 Carousel 檢視畫面,並讓中央項目放大:

這個基本版面配置包含多個代表 Carousel 項目的檢視畫面:

建立具有以下三個狀態的 MotionLayout,並提供這些 ID:

  • 上一步
  • 開始
  • 繼續

如果「start」狀態對應到基本版面配置,在「上一個」狀態和「下一個」狀態中,Carousel 項目會分別向右和向右移動。

舉例來說,以圖 3 中的五個檢視畫面為例,假設在「start」狀態中,檢視畫面 B、C 和 D 會顯示,而 A 和 E 位於螢幕外。設定上一個狀態,讓 A、B、C 和 D 的位置為 B、C、D 和 E 的位置,檢視畫面會從左到右移動。在「下一個」狀態中,相反的情況是,B、C、D 和 E 移至 A、B、C 和 D 的位置,而檢視畫面會從右到左。如圖 4 所示:

觀看次數必須與原始觀看次數的起點完全一致。Carousel 會將「實際」檢視畫面移回其所在位置,但使用新的相符內容重新初始化,模擬無限集合的元素。下圖顯示這個機制。支付「項目 #」的值:

轉場

在動作場景檔案中定義這三個限制集後,請在「start」和「next」狀態和「start」和「Previous」狀態之間建立兩項轉換。新增 OnSwipe 處理常式,以針對手勢觸發轉換程序,如以下範例所示:

    <Transition
        motion:constraintSetStart="@id/start"
        motion:constraintSetEnd="@+id/next"
        motion:duration="1000"
        android:id="@+id/forward">
        <OnSwipe
            motion:dragDirection="dragLeft"
            motion:touchAnchorSide="left" />
    </Transition>

    <Transition
        motion:constraintSetStart="@+id/start"
        motion:constraintSetEnd="@+id/previous"
        android:id="@+id/backward">
        <OnSwipe
            motion:dragDirection="dragRight"
            motion:touchAnchorSide="right" />
    </Transition>

建立這個基本動作場景後,請在版面配置中加入 Carousel 輔助程式,並按照實作先前和下一個動畫的順序參照檢視畫面。

Carousel 輔助程式設定下列屬性:

  • app:carousel_firstView:代表 Carousel 第一個元素的檢視畫面,在本例中為 C。
  • app:carousel_previousState上一個狀態的 ConstraintSet ID。
  • app:carousel_nextState:「下一個」狀態的 ConstraintSet ID。
  • app:carousel_backwardTransition:在起始先前狀態之間套用的 Transition ID。
  • app:carousel_forwardTransition:套用至起始下一個狀態之間的 Transition ID。

舉例來說,在版面配置 XML 檔案中有以下內容:

    <androidx.constraintlayout.motion.widget.MotionLayout ... >

        <ImageView  android:id="@+id/imageView0" .. />
        <ImageView  android:id="@+id/imageView1" .. />
        <ImageView  android:id="@+id/imageView2" .. />
        <ImageView  android:id="@+id/imageView3" .. />
        <ImageView  android:id="@+id/imageView4" .. />

        <androidx.constraintlayout.helper.widget.Carousel
            android:id="@+id/carousel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:carousel_forwardTransition="@+id/forward"
            app:carousel_backwardTransition="@+id/backward"
            app:carousel_previousState="@+id/previous"
            app:carousel_nextState="@+id/next"
            app:carousel_infinite="true"
            app:carousel_firstView="@+id/imageView2"
            app:constraint_referenced_ids="imageView0,imageView1,imageView2,imageView3,imageView4" />

    </androidx.constraintlayout.motion.widget.MotionLayout>

在程式碼中設定 Carousel 轉接器:

Kotlin

carousel.setAdapter(object : Carousel.Adapter {
            override fun count(): Int {
              // Return the number of items in the Carousel.
            }

            override fun populate(view: View, index: Int) {
                // Implement this to populate the view at the given index.
            }

            override fun onNewItem(index: Int) {
                // Called when an item is set.
            }
        })

Java

carousel.setAdapter(new Carousel.Adapter() {
            @Override
            public int count() {
                // Return the number of items in the Carousel.
            }

            @Override
            public void populate(View view, int index) {
                // Populate the view at the given index.
            }

            @Override
            public void onNewItem(int index) {
                 // Called when an item is set.
            }
        });

其他注意事項

Carousel 中「已選取」的目前項目而定,代表前述項目的檢視畫面可能需要隱藏,才能正確計算 Carousel startendCarousel 輔助程式會自動處理這項作業。在這類情況下,它預設會將這些檢視畫面標示為 View.INVISIBLE,因此整體版面配置不會改變。

Carousel 輔助程式會提供替代模式,將這些檢視畫面標示為 View.GONE。您可以利用下列屬性設定這個模式:

app:carousel_emptyViewsBehavior="gone"

範例

如需更多使用輪轉介面輔助工具的範例,請參閱 GitHub 上的專案範例