RecyclerView को लेज़ी लिस्ट पर माइग्रेट करना

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

इसके लिए, लेज़ी लिस्ट का इस्तेमाल भी किया जा सकता है. यह पेज यह बताता है कि लेज़ी लिस्ट का इस्तेमाल करने के लिए, RecyclerView को लागू करने की प्रोसेस को कैसे माइग्रेट किया जा सकता है लिखें.

माइग्रेशन का तरीका

आपने जो RecyclerView लागू किया है उसे Compose में माइग्रेट करने के लिए, यह तरीका अपनाएं:

  1. टिप्पणी करें या अपने यूज़र इंटरफ़ेस (यूआई) हैरारकी से RecyclerView को हटाएं और अगर क्रम में अब तक कोई भी मौजूद नहीं है, तो इसे बदलने के लिए ComposeView. यह लेज़ी सूची का कंटेनर है, जिसे आप जोड़ना चाहते हैं:

          <FrameLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    
      <!--    <androidx.recyclerview.widget.RecyclerView-->
      <!--            android:id="@+id/recycler_view"-->
      <!--            android:layout_width="match_parent"-->
      <!--            android:layout_height="match_parent />"-->
    
              <androidx.compose.ui.platform.ComposeView
                  android:id="@+id/compose_view"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" />
    
          </FrameLayout>
    
  2. अपने हिसाब से तय करें कि आपको किस तरह की लेज़ी लिस्ट की ज़रूरत है RecyclerView का लेआउट मैनेजर (नीचे दी गई टेबल देखें). आपका चुना गया कंपोज़ेबल यह उस ComposeView का टॉप-लेवल कंपोज़ेबल होगा जिसे आपने इसमें जोड़ा है पिछला चरण.

    LayoutManager

    आसानी से मैनेज और छोटे किए जा सकने वाले डिवाइस

    LinearLayoutManager

    LazyColumn या LazyRow

    GridLayoutManager

    LazyVerticalGrid या LazyHorizontalGrid

    StaggeredGridLayoutManager

    LazyVerticalStaggeredGrid या LazyHorizontalStaggeredGrid

    // recyclerView.layoutManager = LinearLayoutManager(context)
    composeView.setContent {
        LazyColumn(Modifier.fillMaxSize()) {
            // We use a LazyColumn since the layout manager of the RecyclerView is a vertical LinearLayoutManager
        }
    }

  3. हर व्यू टाइप के हिसाब से कंपोज़ेबल बनाएं: RecyclerView.Adapter लागू करना. हर व्यू टाइप के लिए, आम तौर पर ViewHolder सब-क्लास. हालांकि, ऐसा हो सकता है कि हमेशा ऐसा न हो. ये कंपोज़ेबल का इस्तेमाल अलग-अलग टाइप के आपकी सूची के तत्व:

    @Composable
    fun ListItem(data: MyData, modifier: Modifier = Modifier) {
        Row(modifier.fillMaxWidth()) {
            Text(text = data.name)
            // … other composables required for displaying `data`
        }
    }

    RecyclerView.Adapter की onCreateViewHolder() और onBindViewHolder() तरीकों को इन कंपोज़ेबल और यह नहीं बताया जाएगा कि आपने उन्हें उपलब्ध कराया है. Compose में, अलग-अलग वैल्यू के हिसाब से किसी आइटम के लिए कंपोज़ेबल और उसमें बाइंडिंग डेटा बनाना. ये कॉन्सेप्ट हैं आपस में जुड़े हुए.

  4. लेज़ी लिस्ट के content स्लॉट (पीछे में मौजूद लैम्डा पैरामीटर) के अंदर, items() फ़ंक्शन (या बराबर के ओवरलोड) का इस्तेमाल करके, डेटा शामिल करें. itemContent Lambda फ़ंक्शन में, सही वैल्यू शुरू करें कंपोज़ेबल आइटम:

    val data = listOf<MyData>(/* ... */)
    composeView.setContent {
        LazyColumn(Modifier.fillMaxSize()) {
            items(data) {
                ListItem(it)
            }
        }
    }

इस्तेमाल के सामान्य उदाहरण

सजावट का सामान

RecyclerView में ItemDecoration का कॉन्सेप्ट है, जिसका इस्तेमाल करके सूची में मौजूद आइटम के लिए खास ड्रॉइंग. उदाहरण के लिए, आप आइटम के बीच डिवाइडर जोड़ने के लिए ItemDecoration:

val itemDecoration = DividerItemDecoration(recyclerView.context, LinearLayoutManager.VERTICAL)
recyclerView.addItemDecoration(itemDecoration)

'लिखें' फ़ील्ड में, आइटम को सजाने का कॉन्सेप्ट एक जैसा नहीं है. इसके बजाय, आप कंपोज़िशन में सीधे तौर पर सूची में कोई भी यूज़र इंटरफ़ेस (यूआई) जोड़ा जा सकता है. उदाहरण के लिए, सूची में डिवाइडर जोड़ने के लिए, हर टाइप के बाद Divider कंपोज़ेबल का इस्तेमाल करें आइटम:

LazyColumn(Modifier.fillMaxSize()) {
    itemsIndexed(data) { index, d ->
        ListItem(d)
        if (index != data.size - 1) {
            Divider()
        }
    }
}

आइटम पर आधारित ऐनिमेशन

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

animateItemPlacement मॉडिफ़ायर में, लेज़ी सूचियों का भी यही सिद्धांत है. ज़्यादा जानने के लिए, आइटम के ऐनिमेशन देखें.

अन्य संसाधन

RecyclerView को 'लिखें' फ़ोल्डर में माइग्रेट करने के बारे में ज़्यादा जानने के लिए, इन संसाधनों की मदद से:

{% endverba नया %} {% verbatim %}