สูตรแผงสนับสนุนเนื้อหา

สูตรนี้แสดงวิธีสร้างเลย์เอาต์ที่ปรับเปลี่ยนได้โดยมีแผงหลักและแผงสนับสนุนโดยใช้ SupportingPaneSceneStrategy จากไลบรารีการปรับเปลี่ยนได้ของ Material 3 เลย์เอาต์นี้มีประโยชน์ในการแสดงเนื้อหาเสริมควบคู่ไปกับเนื้อหาหลักบนหน้าจอขนาดใหญ่

วิธีการทำงาน

ตัวอย่างนี้มีปลายทาง 3 แห่ง ได้แก่ MainVideo, RelatedVideos และ Profile

SupportingPaneSceneStrategy

rememberSupportingPaneSceneStrategy มีตรรกะสําหรับเลย์เอาต์แบบปรับอัตโนมัตินี้

  • บทบาทของแผง: ระบบจะกำหนดบทบาทให้กับปลายทางแต่ละแห่งโดยใช้ข้อมูลเมตา ดังนี้

    • SupportingPaneSceneStrategy.mainPane(): สำหรับเนื้อหาหลัก แผงนี้จะปรากฏอยู่เสมอ
    • SupportingPaneSceneStrategy.supportingPane(): สำหรับเนื้อหาเสริม แผงนี้จะแสดงข้างแผงหลักบนหน้าจอขนาดใหญ่
    • SupportingPaneSceneStrategy.extraPane(): สำหรับเนื้อหาระดับที่ 3 ที่แสดงควบคู่กับแผงสนับสนุนในหน้าจอที่ใหญ่ขึ้น
  • เลย์เอาต์ที่ปรับเปลี่ยนได้: SupportingPaneSceneStrategy จะจัดการเลย์เอาต์โดยอัตโนมัติ ในหน้าจอขนาดเล็ก ระบบจะแสดงเฉพาะแผงหลัก ในหน้าจอขนาดใหญ่กว่า บานหน้าต่างสนับสนุนจะแสดงข้างบานหน้าต่างหลัก

  • การนำทางย้อนกลับ: BackNavigationBehavior ได้รับการปรับแต่งในตัวอย่างนี้เป็น PopUntilCurrentDestinationChange ซึ่งหมายความว่าเมื่อผู้ใช้กดปุ่มย้อนกลับ บานหน้าต่างสนับสนุนจะปิดลงและแสดงบานหน้าต่างหลักที่อยู่ด้านล่าง

  • การนำทาง: การนำทางจะได้รับการจัดการโดยการเพิ่มและนำปลายทางออกจากสแต็กย้อนกลับ SupportingPaneSceneStrategy จะสังเกตการเปลี่ยนแปลงเหล่านี้และปรับเลย์เอาต์ตามนั้น

package com.example.nav3recipes.material.supportingpane

/*
 * Copyright 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfoV2
import androidx.compose.material3.adaptive.layout.calculatePaneScaffoldDirective
import androidx.compose.material3.adaptive.navigation.BackNavigationBehavior
import androidx.compose.material3.adaptive.navigation3.SupportingPaneSceneStrategy
import androidx.compose.material3.adaptive.navigation3.rememberSupportingPaneSceneStrategy
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.dropUnlessResumed
import androidx.navigation3.runtime.NavKey
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.runtime.rememberNavBackStack
import androidx.navigation3.ui.NavDisplay
import com.example.nav3recipes.content.ContentBlue
import com.example.nav3recipes.content.ContentGreen
import com.example.nav3recipes.content.ContentRed
import com.example.nav3recipes.ui.setEdgeToEdgeConfig
import kotlinx.serialization.Serializable

@Serializable
private object MainVideo : NavKey

@Serializable
private data object RelatedVideos : NavKey

@Serializable
private data object Profile : NavKey

class MaterialSupportingPaneActivity : ComponentActivity() {

    @OptIn(ExperimentalMaterial3AdaptiveApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        setEdgeToEdgeConfig()
        super.onCreate(savedInstanceState)

        setContent {

            val backStack = rememberNavBackStack(MainVideo)

            // Override the defaults so that there isn't a horizontal or vertical space between the panes.
            // See b/444438086
            val windowAdaptiveInfo = currentWindowAdaptiveInfoV2()
            val directive = remember(windowAdaptiveInfo) {
                calculatePaneScaffoldDirective(windowAdaptiveInfo)
                    .copy(horizontalPartitionSpacerSize = 0.dp, verticalPartitionSpacerSize = 0.dp)
            }

            // Override the defaults so that the supporting pane can be dismissed by pressing back.
            // See b/445826749
            val supportingPaneStrategy = rememberSupportingPaneSceneStrategy<NavKey>(
                backNavigationBehavior = BackNavigationBehavior.PopUntilCurrentDestinationChange,
                directive = directive
            )

            NavDisplay(
                backStack = backStack,
                onBack = { backStack.removeLastOrNull() },
                sceneStrategies = listOf(supportingPaneStrategy),
                entryProvider = entryProvider {
                    entry<MainVideo>(
                        metadata = SupportingPaneSceneStrategy.mainPane()
                    ) {
                        ContentRed("Video content") {
                            Button(onClick = dropUnlessResumed {
                                backStack.add(RelatedVideos)
                            }) {
                                Text("View related videos")
                            }
                        }
                    }
                    entry<RelatedVideos>(
                        metadata = SupportingPaneSceneStrategy.supportingPane()
                    ) {
                        ContentBlue("Related videos") {
                            Column(horizontalAlignment = Alignment.CenterHorizontally) {
                                Button(onClick = dropUnlessResumed {
                                    backStack.add(Profile)
                                }) {
                                    Text("View profile")
                                }
                            }
                        }
                    }
                    entry<Profile>(
                        metadata = SupportingPaneSceneStrategy.extraPane()
                    ) {
                        ContentGreen("Profile")
                    }
                }
            )
        }
    }
}