टीवी के लिए स्क्रोल किए जा सकने वाले लेआउट बनाना

टीवी ऐप्लिकेशन के लिए, ब्राउज़िंग का अनुभव बेहतर बनाने के लिए, फ़ोकस-आधारित नेविगेशन का इस्तेमाल किया जाता है. स्टैंडर्ड Compose Foundation लेज़ी लेआउट का इस्तेमाल करके, बेहतर परफ़ॉर्मेंस वाली वर्टिकल और हॉरिज़ॉन्टल सूचियां बनाई जा सकती हैं. ये सूचियां, फ़ोकस के हिसाब से स्क्रोल करने की सुविधा को अपने-आप मैनेज करती हैं, ताकि चालू आइटम दिखते रहें.

टीवी के लिए ऑप्टिमाइज़ किया गया डिफ़ॉल्ट स्क्रोल बिहेवियर

Compose Foundation 1.7.0 से, स्टैंडर्ड लेज़ी लेआउट (जैसे, LazyRow और LazyColumn) में फ़ोकस-पोज़िशनिंग की सुविधाओं के लिए पहले से मौजूद सहायता शामिल है. टीवी ऐप्लिकेशन के लिए कैटलॉग बनाने का यह तरीका सबसे सही है. इससे फ़ोकस किए गए आइटम, उपयोगकर्ता को आसानी से दिखते हैं और सही जगह पर मौजूद रहते हैं.

स्क्रोल की जा सकने वाली बेसिक सूची लागू करने के लिए, स्टैंडर्ड लेज़ी कॉम्पोनेंट का इस्तेमाल करें. ये कॉम्पोनेंट, डी-पैड नेविगेशन को अपने-आप मैनेज करते हैं और फ़ोकस किए गए आइटम को स्क्रीन पर दिखाते हैं.

import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items

@Composable
fun MovieCatalog(movies: List<Movie>) {
    LazyRow {
        items(movies) { movie ->
            MovieCard(
                movie = movie,
                onClick = { /* Handle click */ }
            )
        }
    }
}

BringIntoViewSpec की मदद से, स्क्रोल करने के तरीके को पसंद के मुताबिक बनाएं

अगर आपके डिज़ाइन के लिए किसी खास "पिवट" पॉइंट की ज़रूरत है (उदाहरण के लिए, फ़ोकस किए गए आइटम को बाएं किनारे से ठीक 30% की दूरी पर रखना), तो BringIntoViewSpec का इस्तेमाल करके, स्क्रोल करने के तरीके को पसंद के मुताबिक बनाया जा सकता है. यह पुराने pivotOffsets फ़ंक्शन की जगह लेता है. इसकी मदद से, यह तय किया जा सकता है कि फ़ोकस किए गए आइटम को दिखाने के लिए, व्यूपोर्ट को कैसे स्क्रोल किया जाए.

1. पसंद के मुताबिक BringIntoViewSpec तय करना

नीचे दिए गए हेल्पर कंपोज़ेबल की मदद से, पैरंट और चाइल्ड फ़्रैक्शन के आधार पर "पिवट" तय किया जा सकता है. parentFraction से यह तय होता है कि आइटम, कंटेनर में कहां दिखेगा. वहीं, childFraction से यह तय होता है कि आइटम का कौनसा हिस्सा उस पॉइंट के साथ अलाइन होगा.

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun PositionFocusedItemInLazyLayout(
    parentFraction: Float = 0.3f,
    childFraction: Float = 0f,
    content: @Composable () -> Unit,
) {
    val bringIntoViewSpec = remember(parentFraction, childFraction) {
        object : BringIntoViewSpec {
            override fun calculateScrollDistance(
                offset: Float,       // Item's initial position
                size: Float,         // Item's size
                containerSize: Float // Container's size
            ): Float {
                // Calculate the offset position of the item's leading edge.
                val initialTargetForLeadingEdge =
                    parentFraction * containerSize - (childFraction * size)
                // If the item fits in the container, and scrolling would cause
                // its trailing edge to be clipped, adjust targetForLeadingEdge
                // to prevent over-scrolling near the end of list.
                val targetForLeadingEdge = if (size <= containerSize &&
                    (containerSize - initialTargetForLeadingEdge) < size) {
                    // If clipped, align the item's trailing edge with the
                    // container's trailing edge.
                    containerSize - size
                } else {
                    initialTargetForLeadingEdge
                }
                // Return scroll distance relative to initial item position.
                return offset - targetForLeadingEdge
            }
        }
    }

    // Apply the spec to all scrollables in the hierarchy
    CompositionLocalProvider(
        LocalBringIntoViewSpec provides bringIntoViewSpec,
        content = content,
    )
}

2. कस्टम स्पेक लागू करना

पोज़िशनिंग लागू करने के लिए, अपने लेआउट को हेल्पर के साथ रैप करें. यह आपके कैटलॉग की अलग-अलग पंक्तियों में "एक जैसी फ़ोकस लाइन" बनाने के लिए मददगार है.

PositionFocusedItemInLazyLayout(
    parentFraction = 0.3f, // Pivot 30% from the edge
    childFraction = 0.5f   // Center of the item aligns with the pivot
) {
    LazyColumn {
        items(sectionList) { section ->
            // This row and its items will respect the 30% pivot
            LazyRow { /* ... */ }
        }
    }
}

3. नेस्ट किए गए कुछ लेआउट के लिए ऑप्ट-आउट करना

अगर आपके पास कोई ऐसा नेस्ट किया गया लेआउट है जिसमें कस्टम पिवट के बजाय, स्टैंडर्ड स्क्रोलिंग व्यवहार का इस्तेमाल किया जाना चाहिए, तो DefaultBringIntoViewSpec दें:

private val DefaultBringIntoViewSpec = object : BringIntoViewSpec {}

PositionFocusedItemInLazyLayout {
    LazyColumn {
        item {
            // This row will ignore the custom pivot and use default behavior
            CompositionLocalProvider(LocalBringIntoViewSpec provides DefaultBringIntoViewSpec) {
                LazyRow { /* ... */ }
            }
        }
    }
}

BringIntoViewSpec को खाली छोड़ने पर, फ़्रेमवर्क का डिफ़ॉल्ट ऐक्शन और सेटिंग लागू हो जाती है.

TV Foundation से Compose Foundation पर माइग्रेट करना

androidx.tv.foundation में टीवी के लिए उपलब्ध लेज़ी लेआउट अब काम नहीं करते. इनकी जगह, स्टैंडर्ड Compose Foundation लेआउट का इस्तेमाल किया जाता है.

डिपेंडेंसी से जुड़े अपडेट

पुष्टि करें कि आपका build.gradle, 1.7.0 या इसके बाद के वर्शन का इस्तेमाल करता हो. ऐसा इन कामों के लिए ज़रूरी है:

  • androidx.compose.foundation
  • androidx.compose.runtime

कॉम्पोनेंट मैपिंग

माइग्रेट करने के लिए, अपने इंपोर्ट अपडेट करें और अपने कॉम्पोनेंट से Tv प्रीफ़िक्स हटाएं:

अब इस्तेमाल नहीं किया जा सकने वाला टीवी कॉम्पोनेंट Compose Foundation की जगह इस्तेमाल होने वाली लाइब्रेरी
TvLazyRow LazyRow
TvLazyColumn LazyColumn
TvLazyHorizontalGrid LazyHorizontalGrid
TvLazyVerticalGrid LazyVerticalGrid
pivotOffsets BringIntoViewSpec (LocalBringIntoViewSpec के ज़रिए)