عناصر به اشتراک گذاشته شده با Navigation Compose

برای استفاده از عناصر به اشتراک گذاشته شده با وابستگی navigation-compose ، می توانید از همان مفاهیمی که قبلا نشان داده شده است استفاده کنید: از Modifier.sharedElement() استفاده کنید که یک AnimatedVisibilityScope را به عنوان پارامتر می گیرد. می توانید از این برای تصمیم گیری در مورد اینکه چه چیزی و چه زمانی قابل مشاهده باشد استفاده کنید.

شکل 1. پیمایش نوشتن با عناصر مشترک

مثال زیر نمونه ای از استفاده از navigation-compose با عناصر مشترک است:

@Preview
@Composable
fun SharedElement_PredictiveBack() {
   
SharedTransitionLayout {
       
val navController = rememberNavController()
       
NavHost(
            navController
= navController,
            startDestination
= "home"
       
) {
            composable
("home") {
               
HomeScreen(
                    navController
,
                   
this@SharedTransitionLayout,
                   
this@composable
               
)
           
}
            composable
(
               
"details/{item}",
                arguments
= listOf(navArgument("item") { type = NavType.IntType })
           
) { backStackEntry ->
               
val id = backStackEntry.arguments?.getInt("item")
               
val snack = listSnacks[id!!]
               
DetailsScreen(
                    navController
,
                    id
,
                    snack
,
                   
this@SharedTransitionLayout,
                   
this@composable
               
)
           
}
       
}
   
}
}

@Composable
private fun DetailsScreen(
    navController
: NavHostController,
    id
: Int,
    snack
: Snack,
    sharedTransitionScope
: SharedTransitionScope,
    animatedContentScope
: AnimatedContentScope
) {
    with
(sharedTransitionScope) {
       
Column(
           
Modifier
               
.fillMaxSize()
               
.clickable {
                    navController
.navigate("home")
               
}
       
) {
           
Image(
                painterResource
(id = snack.image),
                contentDescription
= snack.description,
                contentScale
= ContentScale.Crop,
                modifier
= Modifier
                   
.sharedElement(
                        sharedTransitionScope
.rememberSharedContentState(key = "image-$id"),
                        animatedVisibilityScope
= animatedContentScope
                   
)
                   
.aspectRatio(1f)
                   
.fillMaxWidth()
           
)
           
Text(
                snack
.name, fontSize = 18.sp,
                modifier
=
               
Modifier
                   
.sharedElement(
                        sharedTransitionScope
.rememberSharedContentState(key = "text-$id"),
                        animatedVisibilityScope
= animatedContentScope
                   
)
                   
.fillMaxWidth()
           
)
       
}
   
}
}

@Composable
private fun HomeScreen(
    navController
: NavHostController,
    sharedTransitionScope
: SharedTransitionScope,
    animatedContentScope
: AnimatedContentScope
) {
   
LazyColumn(
        modifier
= Modifier
           
.fillMaxSize()
           
.padding(8.dp),
        verticalArrangement
= Arrangement.spacedBy(8.dp)
   
) {
        itemsIndexed
(listSnacks) { index, item ->
           
Row(
               
Modifier.clickable {
                    navController
.navigate("details/$index")
               
}
           
) {
               
Spacer(modifier = Modifier.width(8.dp))
                with
(sharedTransitionScope) {
                   
Image(
                        painterResource
(id = item.image),
                        contentDescription
= item.description,
                        contentScale
= ContentScale.Crop,
                        modifier
= Modifier
                           
.sharedElement(
                                sharedTransitionScope
.rememberSharedContentState(key = "image-$index"),
                                animatedVisibilityScope
= animatedContentScope
                           
)
                           
.size(100.dp)
                   
)
                   
Spacer(modifier = Modifier.width(8.dp))
                   
Text(
                        item
.name, fontSize = 18.sp,
                        modifier
= Modifier
                           
.align(Alignment.CenterVertically)
                           
.sharedElement(
                                sharedTransitionScope
.rememberSharedContentState(key = "text-$index"),
                                animatedVisibilityScope
= animatedContentScope,
                           
)
                   
)
               
}
           
}
       
}
   
}
}

data class Snack(
   
val name: String,
   
val description: String,
   
@DrawableRes val image: Int
)

بازگشت پیشگو با عناصر مشترک

اگر می‌خواهید از پیش‌بینی‌کننده با عناصر به اشتراک‌گذاشته‌شده استفاده کنید، مطمئن شوید که از آخرین وابستگی navigation-compose استفاده می‌کنید، با استفاده از قطعه‌ای از بخش قبل.

dependencies {
   
def nav_version = "2.8.0-beta02"

    implementation
"androidx.navigation:navigation-compose:$nav_version"
}

android:enableOnBackInvokedCallback="true" را به فایل AndroidManifest.xml خود اضافه کنید تا بازگشت پیشگویانه را فعال کنید:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
   
<uses-permission android:name="android.permission.INTERNET" />
   
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
       
android:maxSdkVersion="28" />

   
<application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
android:roundIcon="@mipmap/ic_launcher_round"
       
android:supportsRtl="true"
       
android:enableOnBackInvokedCallback="true"
       
android:theme="@style/Theme.Snippets">
شکل 2. پیمایش نوشتن با پیشگویانه
،

برای استفاده از عناصر به اشتراک گذاشته شده با وابستگی navigation-compose ، می توانید از همان مفاهیمی که قبلا نشان داده شده است استفاده کنید: از Modifier.sharedElement() استفاده کنید که یک AnimatedVisibilityScope را به عنوان پارامتر می گیرد. می توانید از این برای تصمیم گیری در مورد اینکه چه چیزی و چه زمانی قابل مشاهده باشد استفاده کنید.

شکل 1. پیمایش نوشتن با عناصر مشترک

مثال زیر نمونه ای از استفاده از navigation-compose با عناصر مشترک است:

@Preview
@Composable
fun SharedElement_PredictiveBack() {
   
SharedTransitionLayout {
       
val navController = rememberNavController()
       
NavHost(
            navController
= navController,
            startDestination
= "home"
       
) {
            composable
("home") {
               
HomeScreen(
                    navController
,
                   
this@SharedTransitionLayout,
                   
this@composable
               
)
           
}
            composable
(
               
"details/{item}",
                arguments
= listOf(navArgument("item") { type = NavType.IntType })
           
) { backStackEntry ->
               
val id = backStackEntry.arguments?.getInt("item")
               
val snack = listSnacks[id!!]
               
DetailsScreen(
                    navController
,
                    id
,
                    snack
,
                   
this@SharedTransitionLayout,
                   
this@composable
               
)
           
}
       
}
   
}
}

@Composable
private fun DetailsScreen(
    navController
: NavHostController,
    id
: Int,
    snack
: Snack,
    sharedTransitionScope
: SharedTransitionScope,
    animatedContentScope
: AnimatedContentScope
) {
    with
(sharedTransitionScope) {
       
Column(
           
Modifier
               
.fillMaxSize()
               
.clickable {
                    navController
.navigate("home")
               
}
       
) {
           
Image(
                painterResource
(id = snack.image),
                contentDescription
= snack.description,
                contentScale
= ContentScale.Crop,
                modifier
= Modifier
                   
.sharedElement(
                        sharedTransitionScope
.rememberSharedContentState(key = "image-$id"),
                        animatedVisibilityScope
= animatedContentScope
                   
)
                   
.aspectRatio(1f)
                   
.fillMaxWidth()
           
)
           
Text(
                snack
.name, fontSize = 18.sp,
                modifier
=
               
Modifier
                   
.sharedElement(
                        sharedTransitionScope
.rememberSharedContentState(key = "text-$id"),
                        animatedVisibilityScope
= animatedContentScope
                   
)
                   
.fillMaxWidth()
           
)
       
}
   
}
}

@Composable
private fun HomeScreen(
    navController
: NavHostController,
    sharedTransitionScope
: SharedTransitionScope,
    animatedContentScope
: AnimatedContentScope
) {
   
LazyColumn(
        modifier
= Modifier
           
.fillMaxSize()
           
.padding(8.dp),
        verticalArrangement
= Arrangement.spacedBy(8.dp)
   
) {
        itemsIndexed
(listSnacks) { index, item ->
           
Row(
               
Modifier.clickable {
                    navController
.navigate("details/$index")
               
}
           
) {
               
Spacer(modifier = Modifier.width(8.dp))
                with
(sharedTransitionScope) {
                   
Image(
                        painterResource
(id = item.image),
                        contentDescription
= item.description,
                        contentScale
= ContentScale.Crop,
                        modifier
= Modifier
                           
.sharedElement(
                                sharedTransitionScope
.rememberSharedContentState(key = "image-$index"),
                                animatedVisibilityScope
= animatedContentScope
                           
)
                           
.size(100.dp)
                   
)
                   
Spacer(modifier = Modifier.width(8.dp))
                   
Text(
                        item
.name, fontSize = 18.sp,
                        modifier
= Modifier
                           
.align(Alignment.CenterVertically)
                           
.sharedElement(
                                sharedTransitionScope
.rememberSharedContentState(key = "text-$index"),
                                animatedVisibilityScope
= animatedContentScope,
                           
)
                   
)
               
}
           
}
       
}
   
}
}

data class Snack(
   
val name: String,
   
val description: String,
   
@DrawableRes val image: Int
)

بازگشت پیشگو با عناصر مشترک

اگر می‌خواهید از پیش‌بینی‌کننده با عناصر به اشتراک‌گذاشته‌شده استفاده کنید، مطمئن شوید که از آخرین وابستگی navigation-compose استفاده می‌کنید، با استفاده از قطعه‌ای از بخش قبل.

dependencies {
   
def nav_version = "2.8.0-beta02"

    implementation
"androidx.navigation:navigation-compose:$nav_version"
}

android:enableOnBackInvokedCallback="true" را به فایل AndroidManifest.xml خود اضافه کنید تا بازگشت پیشگویانه را فعال کنید:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
   
<uses-permission android:name="android.permission.INTERNET" />
   
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
       
android:maxSdkVersion="28" />

   
<application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
android:roundIcon="@mipmap/ic_launcher_round"
       
android:supportsRtl="true"
       
android:enableOnBackInvokedCallback="true"
       
android:theme="@style/Theme.Snippets">
شکل 2. پیمایش نوشتن با پیشگویانه
،

برای استفاده از عناصر به اشتراک گذاشته شده با وابستگی navigation-compose ، می توانید از همان مفاهیمی که قبلا نشان داده شده است استفاده کنید: از Modifier.sharedElement() استفاده کنید که یک AnimatedVisibilityScope را به عنوان پارامتر می گیرد. می توانید از این برای تصمیم گیری در مورد اینکه چه چیزی و چه زمانی قابل مشاهده باشد استفاده کنید.

شکل 1. پیمایش نوشتن با عناصر مشترک

مثال زیر نمونه ای از استفاده از navigation-compose با عناصر مشترک است:

@Preview
@Composable
fun SharedElement_PredictiveBack() {
   
SharedTransitionLayout {
       
val navController = rememberNavController()
       
NavHost(
            navController
= navController,
            startDestination
= "home"
       
) {
            composable
("home") {
               
HomeScreen(
                    navController
,
                   
this@SharedTransitionLayout,
                   
this@composable
               
)
           
}
            composable
(
               
"details/{item}",
                arguments
= listOf(navArgument("item") { type = NavType.IntType })
           
) { backStackEntry ->
               
val id = backStackEntry.arguments?.getInt("item")
               
val snack = listSnacks[id!!]
               
DetailsScreen(
                    navController
,
                    id
,
                    snack
,
                   
this@SharedTransitionLayout,
                   
this@composable
               
)
           
}
       
}
   
}
}

@Composable
private fun DetailsScreen(
    navController
: NavHostController,
    id
: Int,
    snack
: Snack,
    sharedTransitionScope
: SharedTransitionScope,
    animatedContentScope
: AnimatedContentScope
) {
    with
(sharedTransitionScope) {
       
Column(
           
Modifier
               
.fillMaxSize()
               
.clickable {
                    navController
.navigate("home")
               
}
       
) {
           
Image(
                painterResource
(id = snack.image),
                contentDescription
= snack.description,
                contentScale
= ContentScale.Crop,
                modifier
= Modifier
                   
.sharedElement(
                        sharedTransitionScope
.rememberSharedContentState(key = "image-$id"),
                        animatedVisibilityScope
= animatedContentScope
                   
)
                   
.aspectRatio(1f)
                   
.fillMaxWidth()
           
)
           
Text(
                snack
.name, fontSize = 18.sp,
                modifier
=
               
Modifier
                   
.sharedElement(
                        sharedTransitionScope
.rememberSharedContentState(key = "text-$id"),
                        animatedVisibilityScope
= animatedContentScope
                   
)
                   
.fillMaxWidth()
           
)
       
}
   
}
}

@Composable
private fun HomeScreen(
    navController
: NavHostController,
    sharedTransitionScope
: SharedTransitionScope,
    animatedContentScope
: AnimatedContentScope
) {
   
LazyColumn(
        modifier
= Modifier
           
.fillMaxSize()
           
.padding(8.dp),
        verticalArrangement
= Arrangement.spacedBy(8.dp)
   
) {
        itemsIndexed
(listSnacks) { index, item ->
           
Row(
               
Modifier.clickable {
                    navController
.navigate("details/$index")
               
}
           
) {
               
Spacer(modifier = Modifier.width(8.dp))
                with
(sharedTransitionScope) {
                   
Image(
                        painterResource
(id = item.image),
                        contentDescription
= item.description,
                        contentScale
= ContentScale.Crop,
                        modifier
= Modifier
                           
.sharedElement(
                                sharedTransitionScope
.rememberSharedContentState(key = "image-$index"),
                                animatedVisibilityScope
= animatedContentScope
                           
)
                           
.size(100.dp)
                   
)
                   
Spacer(modifier = Modifier.width(8.dp))
                   
Text(
                        item
.name, fontSize = 18.sp,
                        modifier
= Modifier
                           
.align(Alignment.CenterVertically)
                           
.sharedElement(
                                sharedTransitionScope
.rememberSharedContentState(key = "text-$index"),
                                animatedVisibilityScope
= animatedContentScope,
                           
)
                   
)
               
}
           
}
       
}
   
}
}

data class Snack(
   
val name: String,
   
val description: String,
   
@DrawableRes val image: Int
)

بازگشت پیشگو با عناصر مشترک

اگر می‌خواهید از پیش‌بینی‌کننده با عناصر به اشتراک‌گذاشته‌شده استفاده کنید، مطمئن شوید که از آخرین وابستگی navigation-compose استفاده می‌کنید، با استفاده از قطعه‌ای از بخش قبل.

dependencies {
   
def nav_version = "2.8.0-beta02"

    implementation
"androidx.navigation:navigation-compose:$nav_version"
}

android:enableOnBackInvokedCallback="true" را به فایل AndroidManifest.xml خود اضافه کنید تا بازگشت پیشگویانه را فعال کنید:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
   
<uses-permission android:name="android.permission.INTERNET" />
   
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
       
android:maxSdkVersion="28" />

   
<application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
android:roundIcon="@mipmap/ic_launcher_round"
       
android:supportsRtl="true"
       
android:enableOnBackInvokedCallback="true"
       
android:theme="@style/Theme.Snippets">
شکل 2. پیمایش نوشتن با پیشگویانه
،

برای استفاده از عناصر به اشتراک گذاشته شده با وابستگی navigation-compose ، می توانید از همان مفاهیمی که قبلا نشان داده شده است استفاده کنید: از Modifier.sharedElement() استفاده کنید که یک AnimatedVisibilityScope را به عنوان پارامتر می گیرد. می توانید از این برای تصمیم گیری در مورد اینکه چه چیزی و چه زمانی قابل مشاهده باشد استفاده کنید.

شکل 1. پیمایش نوشتن با عناصر مشترک

مثال زیر نمونه ای از استفاده از navigation-compose با عناصر مشترک است:

@Preview
@Composable
fun SharedElement_PredictiveBack() {
   
SharedTransitionLayout {
       
val navController = rememberNavController()
       
NavHost(
            navController
= navController,
            startDestination
= "home"
       
) {
            composable
("home") {
               
HomeScreen(
                    navController
,
                   
this@SharedTransitionLayout,
                   
this@composable
               
)
           
}
            composable
(
               
"details/{item}",
                arguments
= listOf(navArgument("item") { type = NavType.IntType })
           
) { backStackEntry ->
               
val id = backStackEntry.arguments?.getInt("item")
               
val snack = listSnacks[id!!]
               
DetailsScreen(
                    navController
,
                    id
,
                    snack
,
                   
this@SharedTransitionLayout,
                   
this@composable
               
)
           
}
       
}
   
}
}

@Composable
private fun DetailsScreen(
    navController
: NavHostController,
    id
: Int,
    snack
: Snack,
    sharedTransitionScope
: SharedTransitionScope,
    animatedContentScope
: AnimatedContentScope
) {
    with
(sharedTransitionScope) {
       
Column(
           
Modifier
               
.fillMaxSize()
               
.clickable {
                    navController
.navigate("home")
               
}
       
) {
           
Image(
                painterResource
(id = snack.image),
                contentDescription
= snack.description,
                contentScale
= ContentScale.Crop,
                modifier
= Modifier
                   
.sharedElement(
                        sharedTransitionScope
.rememberSharedContentState(key = "image-$id"),
                        animatedVisibilityScope
= animatedContentScope
                   
)
                   
.aspectRatio(1f)
                   
.fillMaxWidth()
           
)
           
Text(
                snack
.name, fontSize = 18.sp,
                modifier
=
               
Modifier
                   
.sharedElement(
                        sharedTransitionScope
.rememberSharedContentState(key = "text-$id"),
                        animatedVisibilityScope
= animatedContentScope
                   
)
                   
.fillMaxWidth()
           
)
       
}
   
}
}

@Composable
private fun HomeScreen(
    navController
: NavHostController,
    sharedTransitionScope
: SharedTransitionScope,
    animatedContentScope
: AnimatedContentScope
) {
   
LazyColumn(
        modifier
= Modifier
           
.fillMaxSize()
           
.padding(8.dp),
        verticalArrangement
= Arrangement.spacedBy(8.dp)
   
) {
        itemsIndexed
(listSnacks) { index, item ->
           
Row(
               
Modifier.clickable {
                    navController
.navigate("details/$index")
               
}
           
) {
               
Spacer(modifier = Modifier.width(8.dp))
                with
(sharedTransitionScope) {
                   
Image(
                        painterResource
(id = item.image),
                        contentDescription
= item.description,
                        contentScale
= ContentScale.Crop,
                        modifier
= Modifier
                           
.sharedElement(
                                sharedTransitionScope
.rememberSharedContentState(key = "image-$index"),
                                animatedVisibilityScope
= animatedContentScope
                           
)
                           
.size(100.dp)
                   
)
                   
Spacer(modifier = Modifier.width(8.dp))
                   
Text(
                        item
.name, fontSize = 18.sp,
                        modifier
= Modifier
                           
.align(Alignment.CenterVertically)
                           
.sharedElement(
                                sharedTransitionScope
.rememberSharedContentState(key = "text-$index"),
                                animatedVisibilityScope
= animatedContentScope,
                           
)
                   
)
               
}
           
}
       
}
   
}
}

data class Snack(
   
val name: String,
   
val description: String,
   
@DrawableRes val image: Int
)

بازگشت پیشگو با عناصر مشترک

اگر می‌خواهید از پیش‌بینی‌کننده با عناصر به اشتراک‌گذاشته‌شده استفاده کنید، مطمئن شوید که از آخرین وابستگی navigation-compose استفاده می‌کنید، با استفاده از قطعه‌ای از بخش قبل.

dependencies {
   
def nav_version = "2.8.0-beta02"

    implementation
"androidx.navigation:navigation-compose:$nav_version"
}

android:enableOnBackInvokedCallback="true" را به فایل AndroidManifest.xml خود اضافه کنید تا بازگشت پیشگویانه را فعال کنید:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
   
<uses-permission android:name="android.permission.INTERNET" />
   
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
       
android:maxSdkVersion="28" />

   
<application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
android:roundIcon="@mipmap/ic_launcher_round"
       
android:supportsRtl="true"
       
android:enableOnBackInvokedCallback="true"
       
android:theme="@style/Theme.Snippets">
شکل 2. پیمایش نوشتن با پیشگویانه