Slice 範本

本文件詳細說明如何在 Android Jetpack 用於建構 Slice

定義 Slice 範本

配量是以 ListBuilder。清單建構工具 可讓您新增清單中顯示的各種資料列類型。這個 一節將說明每個資料列類型,以及各列的建構方式。

SliceAction

Slice 範本最基本的元素是 SliceActionSliceAction 內含 標籤以及 PendingIntent,是以下任一號: 包括:

  • 圖示按鈕
  • 預設切換鈕
  • 自訂切換 (處於開啟/關閉狀態的可繪項目)

本後續章節中所述的範本建構工具會使用 SliceAction 專區。SliceAction 可以定義圖片模式,決定 。

  • ICON_IMAGE:最小尺寸和可著色
  • SMALL_IMAGE:小型,且不可著色版
  • LARGE_IMAGE:最大尺寸且不可著色版

標頭建構工具

在多數情況下,您應使用 HeaderBuilder。 標頭可支援下列內容:

  • 標題
  • 副標題
  • 摘要副標題
  • 主要動作

以下為部分標頭設定範例。請注意,灰色方塊會顯示 潛在圖示和邊框間距位置:

在不同平台上顯示標頭

需要使用 Slice 時,顯示途徑會決定 Slice。請注意,各代管介面的算繪作業可能略有不同。

在較小的格式中,通常只會顯示標題 (如果有的話)。如果 您為標題指定摘要後,系統會顯示摘要文字,而不是 副標題文字

如果您尚未在範本中指定標頭,請將第一列新增至 通常改為顯示 ListBuilder

HeaderBuilder 範例 - 含標頭的簡易清單 Slice

KotlinJava
fun createSliceWithHeader(sliceUri: Uri) =
    list
(context, sliceUri, ListBuilder.INFINITY) {
        setAccentColor
(0xff0F9D) // Specify color for tinting icons
        header
{
            title
= "Get a ride"
            subtitle
= "Ride in 4 min"
            summary
= "Work in 1 hour 45 min | Home in 12 min"
       
}
        row
{
            title
= "Home"
            subtitle
= "12 miles | 12 min | $9.00"
            addEndItem
(
               
IconCompat.createWithResource(context, R.drawable.ic_home),
               
ListBuilder.ICON_IMAGE
           
)
       
}
   
}
public Slice createSliceWithHeader(Uri sliceUri) {
   
if (getContext() == null) {
       
return null;
   
}

   
// Construct the parent.
   
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY)
           
.setAccentColor(0xff0F9D58) // Specify color for tinting icons.
           
.setHeader( // Create the header and add to slice.
                   
new HeaderBuilder()
                           
.setTitle("Get a ride")
                           
.setSubtitle("Ride in 4 min.")
                           
.setSummary("Work in 1 hour 45 min | Home in 12 min.")
           
).addRow(new RowBuilder() // Add a row.
                   
.setPrimaryAction(
                            createActivityAction
()) // A slice always needs a SliceAction.
                   
.setTitle("Home")
                   
.setSubtitle("12 miles | 12 min | $9.00")
                   
.addEndItem(IconCompat.createWithResource(getContext(), R.drawable.ic_home),
                           
SliceHints.ICON_IMAGE)
           
); // Add more rows if needed...
   
return listBuilder.build();
}

標頭中的 SliceAction

Slice 標頭也會顯示 SliceActions:

KotlinJava
fun createSliceWithActionInHeader(sliceUri: Uri): Slice {
   
// Construct our slice actions.
   
val noteAction = SliceAction.create(
        takeNoteIntent
,
       
IconCompat.createWithResource(context, R.drawable.ic_pencil),
        ICON
_IMAGE,
       
"Take note"
   
)

   
val voiceNoteAction = SliceAction.create(
        voiceNoteIntent
,
       
IconCompat.createWithResource(context, R.drawable.ic_mic),
        ICON
_IMAGE,
       
"Take voice note"
   
)

   
val cameraNoteAction = SliceAction.create(
        cameraNoteIntent
,
       
IconCompat.createWithResource(context, R.drawable.ic_camera),
        ICON
_IMAGE,
       
"Create photo note"
   
)

   
// Construct the list.
   
return list(context, sliceUri, ListBuilder.INFINITY) {
        setAccentColor
(0xfff4b4) // Specify color for tinting icons
        header
{
            title
= "Create new note"
            subtitle
= "Easily done with this note taking app"
       
}
        addAction
(noteAction)
        addAction
(voiceNoteAction)
        addAction
(cameraNoteAction)
   
}
}
public Slice createSliceWithActionInHeader(Uri sliceUri) {
   
if (getContext() == null) {
       
return null;
   
}
   
// Construct our slice actions.
   
SliceAction noteAction = SliceAction.create(takeNoteIntent,
           
IconCompat.createWithResource(getContext(), R.drawable.ic_pencil),
           
ListBuilder.ICON_IMAGE, "Take note");

   
SliceAction voiceNoteAction = SliceAction.create(voiceNoteIntent,
           
IconCompat.createWithResource(getContext(), R.drawable.ic_mic),
           
ListBuilder.ICON_IMAGE,
           
"Take voice note");

   
SliceAction cameraNoteAction = SliceAction.create(cameraNoteIntent,
           
IconCompat.createWithResource(getContext(), R.drawable.ic_camera),
           
ListBuilder.ICON_IMAGE,
           
"Create photo note");


   
// Construct the list.
   
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY)
           
.setAccentColor(0xfff4b400) // Specify color for tinting icons
           
.setHeader(new HeaderBuilder() // Construct the header.
                   
.setTitle("Create new note")
                   
.setSubtitle("Easily done with this note taking app")
           
)
           
.addRow(new RowBuilder()
                   
.setTitle("Enter app")
                   
.setPrimaryAction(createActivityAction())
           
)
           
// Add the actions to the ListBuilder.
           
.addAction(noteAction)
           
.addAction(voiceNoteAction)
           
.addAction(cameraNoteAction);
   
return listBuilder.build();
}

資料列建構工具

您可以使用 RowBuilder。單列 可支援下列任何一項:

  • 標題
  • 副標題
  • 開始項目:SliceAction、Icon 或時間戳記
  • 結束項目:SliceAction、Icon 或時間戳記
  • 主要動作

您可以使用多種方式合併資料列內容,前提是以下項目必須符合下列條件: 限制:

  • 起始項目不會顯示在 Slice 的第一列
  • 結尾項目不得混合使用 SliceAction 物件和 Icon 物件
  • 一列只能包含一個時間戳記

範例列的內容如下圖所示。請注意,灰色方塊 顯示潛在圖示和邊框間距位置:

RowBuilder 範例 - Wi-Fi 切換

以下範例展示了含有主要動作和預設切換鈕的資料列。

KotlinJava
fun createActionWithActionInRow(sliceUri: Uri): Slice {
   
// Primary action - open wifi settings.
   
val wifiAction = SliceAction.create(
        wifiSettingsPendingIntent
,
       
IconCompat.createWithResource(context, R.drawable.ic_wifi),
        ICON
_IMAGE,
       
"Wi-Fi Settings"
   
)

   
// Toggle action - toggle wifi.
   
val toggleAction = SliceAction.createToggle(
        wifiTogglePendingIntent
,
       
"Toggle Wi-Fi",
        isConnected
/* isChecked */
   
)

   
// Create the parent builder.
   
return list(context, wifiUri, ListBuilder.INFINITY) {
        setAccentColor
(0xff4285) // Specify color for tinting icons / controls.
        row
{
            title
= "Wi-Fi"
            primaryAction
= wifiAction
            addEndItem
(toggleAction)
       
}
   
}
}
public Slice createActionWithActionInRow(Uri sliceUri) {
   
if (getContext() == null) {
       
return null;
   
}
   
// Primary action - open wifi settings.
   
SliceAction primaryAction = SliceAction.create(wifiSettingsPendingIntent,
           
IconCompat.createWithResource(getContext(), R.drawable.ic_wifi),
           
ListBuilder.ICON_IMAGE,
           
"Wi-Fi Settings"
   
);

   
// Toggle action - toggle wifi.
   
SliceAction toggleAction = SliceAction.createToggle(wifiTogglePendingIntent,
           
"Toggle Wi-Fi", isConnected /* isChecked */);

   
// Create the parent builder.
   
ListBuilder listBuilder = new ListBuilder(getContext(), wifiUri, ListBuilder.INFINITY)
           
// Specify color for tinting icons / controls.
           
.setAccentColor(0xff4285f4)
           
// Create and add a row.
           
.addRow(new RowBuilder()
                   
.setTitle("Wi-Fi")
                   
.setPrimaryAction(primaryAction)
                   
.addEndItem(toggleAction));
   
// Build the slice.
   
return listBuilder.build();
}

GridBuilder

您可以使用 GridBuilder。格狀清單 支援下列圖片類型:

  • ICON_IMAGE:最小尺寸和可著色
  • SMALL_IMAGE:小型,且不可著色版
  • LARGE_IMAGE:最大尺寸且不可著色版

網格儲存格是以 CellBuilder。A 罩杯 儲存格最多可支援兩行文字和一張圖片。儲存格不得空白。

格線範例如下圖所示:

GridRowBuilder 範例 - 附近的餐廳

以下範例示範包含圖片和文字的格狀列。

KotlinJava
fun createSliceWithGridRow(sliceUri: Uri): Slice {
   
// Create the parent builder.
   
return list(context, sliceUri, ListBuilder.INFINITY) {
        header
{
            title
= "Famous restaurants"
            primaryAction
= SliceAction.create(
                pendingIntent
, icon, ListBuilder.ICON_IMAGE, "Famous restaurants"
           
)
       
}
        gridRow
{
            cell
{
                addImage
(image1, LARGE_IMAGE)
                addTitleText
("Top Restaurant")
                addText
("0.3 mil")
                contentIntent
= intent1
           
}
            cell
{
                addImage
(image2, LARGE_IMAGE)
                addTitleText
("Fast and Casual")
                addText
("0.5 mil")
                contentIntent
= intent2
           
}
            cell
{
                addImage
(image3, LARGE_IMAGE)
                addTitleText
("Casual Diner")
                addText
("0.9 mi")
                contentIntent
= intent3
           
}
            cell
{
                addImage
(image4, LARGE_IMAGE)
                addTitleText
("Ramen Spot")
                addText
("1.2 mi")
                contentIntent
= intent4
           
}
       
}
   
}
}
public Slice createSliceWithGridRow(Uri sliceUri) {
     
if (getContext() == null) {
         
return null;
     
}
     
// Create the parent builder.
     
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY)
             
.setHeader(
                     
// Create the header.
                     
new HeaderBuilder()
                             
.setTitle("Famous restaurants")
                             
.setPrimaryAction(SliceAction
                                     
.create(pendingIntent, icon, ListBuilder.ICON_IMAGE,
                                             
"Famous restaurants"))
             
)
             
// Add a grid row to the list.
             
.addGridRow(new GridRowBuilder()
                     
// Add cells to the grid row.
                     
.addCell(new CellBuilder()
                             
.addImage(image1, ListBuilder.LARGE_IMAGE)
                             
.addTitleText("Top Restaurant")
                             
.addText("0.3 mil")
                             
.setContentIntent(intent1)
                     
).addCell(new CellBuilder()
                             
.addImage(image2, ListBuilder.LARGE_IMAGE)
                             
.addTitleText("Fast and Casual")
                             
.addText("0.5 mil")
                             
.setContentIntent(intent2)
                     
)
                     
.addCell(new CellBuilder()
                             
.addImage(image3, ListBuilder.LARGE_IMAGE)
                             
.addTitleText("Casual Diner")
                             
.addText("0.9 mi")
                             
.setContentIntent(intent3))
                     
.addCell(new CellBuilder()
                             
.addImage(image4, ListBuilder.LARGE_IMAGE)
                             
.addTitleText("Ramen Spot")
                             
.addText("1.2 mi")
                             
.setContentIntent(intent4))
                     
// Every slice needs a primary action.
                     
.setPrimaryAction(createActivityAction())
             
);
     
return listBuilder.build();
 
}

RangeBuilder

內建 RangeBuilder、 您可以建立一個包含進度列或輸入範圍的資料列,例如 做為滑桿

進度和滑桿範例如下圖所示:

RangeBuilder 範例 - Slider

以下範例說明如何建立包含磁碟區的 Slice 使用 InputRangeBuilder 調整滑桿。如要建構進度列,請使用 addRange()

KotlinJava
fun createSliceWithRange(sliceUri: Uri): Slice {
   
return list(context, sliceUri, ListBuilder.INFINITY) {
        inputRange
{
            title
= "Ring Volume"
            inputAction
= volumeChangedPendingIntent
            max
= 100
            value
= 30
       
}
   
}
}
public Slice createSliceWithRange(Uri sliceUri) {
   
if (getContext() == null) {
       
return null;
   
}
   
// Construct the parent.
   
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY)
           
.addRow(new RowBuilder() // Every slice needs a row.
                   
.setTitle("Enter app")
                     
// Every slice needs a primary action.
                   
.setPrimaryAction(createActivityAction())
           
)
           
.addInputRange(new InputRangeBuilder() // Create the input row.
                   
.setTitle("Ring Volume")
                   
.setInputAction(volumeChangedPendingIntent)
                   
.setMax(100)
                   
.setValue(30)
           
);
   
return listBuilder.build();
}

延遲內容

您應盡快從 SliceProvider.onBindSlice()。 耗時的通話可能會造成畫面顯示問題,例如閃爍和突然中斷 調整大小。

如果 Slice 內容無法快速載入,您可以建構 有預留位置內容,在建構工具中註明 正在載入內容。內容準備好顯示後,請呼叫 getContentResolver().notifyChange(sliceUri, null)敬上 接著透過 Slice URI這會導致系統再次呼叫 SliceProvider.onBindSlice(),您可以在這裡用全新方式再次建構 Slice 內容。

延遲內容示例 - 開車上班

下方的「開車上班」列會動態決定前往公司的距離 而且可能無法立即使用。程式碼範例示範了 空副標題做為預留位置:

KotlinJava
fun createSliceShowingLoading(sliceUri: Uri): Slice {
   
// We’re waiting to load the time to work so indicate that on the slice by
   
// setting the subtitle with the overloaded method and indicate true.
   
return list(context, sliceUri, ListBuilder.INFINITY) {
        row
{
            title
= "Ride to work"
            setSubtitle
(null, true)
            addEndItem
(IconCompat.createWithResource(context, R.drawable.ic_work), ICON_IMAGE)
       
}
   
}
}
public Slice createSliceShowingLoading(Uri sliceUri) {
   
if (getContext() == null) {
       
return null;
   
}
   
// Construct the parent.
   
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY)
           
// Construct the row.
           
.addRow(new RowBuilder()
                   
.setPrimaryAction(createActivityAction())
                   
.setTitle("Ride to work")
                   
// We’re waiting to load the time to work so indicate that on the slice by
                   
// setting the subtitle with the overloaded method and indicate true.
                   
.setSubtitle(null, true)
                   
.addEndItem(IconCompat.createWithResource(getContext(), R.drawable.ic_work),
                           
ListBuilder.ICON_IMAGE)
           
);
   
return listBuilder.build();
}

private SliceAction createActivityAction() {
   
return SliceAction.create(
           
PendingIntent.getActivity(
                    getContext
(),
                   
0,
                   
new Intent(getContext(), MainActivity.class),
                   
0
           
),
           
IconCompat.createWithResource(getContext(), R.drawable.ic_home),
           
ListBuilder.ICON_IMAGE,
           
"Enter app"
   
);
}

處理 Slice 中已停用的捲動功能

顯示 Slice 範本的介面可能不支援在 範本。在這種情況下,某些內容可能不會顯示。

舉例來說,假設 Slice 顯示的是 Wi-Fi 網路清單:

如果 Wi-Fi 清單很長,且已停用捲動功能,您就可以新增 [顯示更多] 按鈕,可確保使用者能夠查看 請參考閱讀清單,進一步瞭解 如何選擇 Kubeflow Pipelines SDK 或 TFX如要加入這個按鈕,請使用 addSeeMoreAction()、 如以下範例所示:

KotlinJava
fun seeMoreActionSlice(sliceUri: Uri) =
    list
(context, sliceUri, ListBuilder.INFINITY) {
       
// [START_EXCLUDE]
       
// [END_EXCLUDE]
        setSeeMoreAction
(seeAllNetworksPendingIntent)
       
// [START_EXCLUDE]
       
// [END_EXCLUDE]
   
}
public Slice seeMoreActionSlice(Uri sliceUri) {
   
if (getContext() == null) {
       
return null;
   
}
   
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY);
   
// [START_EXCLUDE]
    listBuilder
.addRow(new RowBuilder()
           
.setTitle("Hello")
           
.setPrimaryAction(createActivityAction())
   
);
   
// [END_EXCLUDE]
    listBuilder
.setSeeMoreAction(seeAllNetworksPendingIntent);
   
// [START_EXCLUDE]
   
// [END_EXCLUDE]
   
return listBuilder.build();
}

如下所示:

輕觸「查看更多」即可傳送 seeAllNetworksPendingIntent

或者,如果您想提供自訂訊息或資料列,建議在文案中加入 RowBuilder:

KotlinJava
fun seeMoreRowSlice(sliceUri: Uri) =
    list
(context, sliceUri, ListBuilder.INFINITY) {
       
// [START_EXCLUDE]
       
// [END_EXCLUDE]
        seeMoreRow
{
            title
= "See all available networks"
            addEndItem
(
               
IconCompat.createWithResource(context, R.drawable.ic_right_caret), ICON_IMAGE
           
)
            primaryAction
= SliceAction.create(
                seeAllNetworksPendingIntent
,
               
IconCompat.createWithResource(context, R.drawable.ic_wifi),
               
ListBuilder.ICON_IMAGE,
               
"Wi-Fi Networks"
           
)
       
}
   
}
public Slice seeMoreRowSlice(Uri sliceUri) {
   
if (getContext() == null) {
       
return null;
   
}
   
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY)
           
// [START_EXCLUDE]
           
.addRow(new RowBuilder()
                   
.setTitle("Hello")
                   
.setPrimaryAction(createActivityAction())
           
)
           
// [END_EXCLUDE]
           
.setSeeMoreRow(new RowBuilder()
                   
.setTitle("See all available networks")
                   
.addEndItem(IconCompat
                                   
.createWithResource(getContext(), R.drawable
                                           
.ic_right_caret),
                           
ListBuilder.ICON_IMAGE)
                   
.setPrimaryAction(SliceAction.create(seeAllNetworksPendingIntent,
                           
IconCompat.createWithResource(getContext(), R.drawable.ic_wifi),
                           
ListBuilder.ICON_IMAGE,
                           
"Wi-Fi Networks"))
           
);
   
// [START_EXCLUDE]
   
// [END_EXCLUDE]
   
return listBuilder.build();
}

只有在符合下列其中一項條件的情況下,系統才會顯示透過這個方法新增的資料列或動作 滿足:

  • Slice 的簡報者停用了檢視畫面的捲動功能
  • 可用空間無法顯示部分資料列

合併範本

您可以合併多個資料列類型,建立內容豐富的動態 Slice。適用對象 Slice 可以包含標題列、只有一張圖片的格狀版面 內含兩個文字儲存格的格狀檢視畫面。

以下是包含標題列的 Slice,以及內含三個儲存格的格狀檢視畫面。