कलेक्शन विजेट का इस्तेमाल करना

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

ये विजेट, RemoteViewsService का इस्तेमाल करके ऐसे कलेक्शन दिखाते हैं जिनका डेटा रिमोट से मिलता है. जैसे, कॉन्टेंट उपलब्ध कराने वाली कंपनी से मिला डेटा. विजेट आपको डेटा, जो नीचे दिए गए किसी एक व्यू टाइप का इस्तेमाल करता है. इन व्यू टाइप को कलेक्शन कहा जाता है व्यू:

ListView
ऐसा व्यू जो वर्टिकल स्क्रोलिंग सूची.
GridView
यह एक ऐसा व्यू है जिसमें आइटम, दो डाइमेंशन वाले स्क्रोलिंग ग्रिड में दिखते हैं.
StackView
एक स्टैक किया गया कार्ड बिलकुल किसी रोलोडेक्स जैसा दिखता है—जिसमें उपयोगकर्ता सामने वाले हिस्से को फ़्लिक कर सकता है पिछला या अगला कार्ड देखने के लिए, कार्ड को ऊपर या नीचे की ओर ले जाएं.
AdapterViewFlipper
एक अडैप्टर-बैक्ड सिंपल ViewAnimator जो ऐनिमेट होता है दो या ज़्यादा व्यू के बीच हो सकता है. एक बार में सिर्फ़ एक ही बच्चा दिखाया जाता है.

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

साथ ही, इन कलेक्शन व्यू को अडैप्टर की मदद से दिखाया जाता है. इसलिए, विजेट में इनका इस्तेमाल करने के लिए, Android फ़्रेमवर्क में अतिरिक्त आर्किटेक्चर शामिल करना ज़रूरी है. इस संदर्भ में तो Adapter RemoteViewsFactory, जो Adapter इंटरफ़ेस के चारों ओर एक पतला रैपर होता है. जब कलेक्शन में किसी खास आइटम का अनुरोध किया जाता है, तो RemoteViewsFactory उस आइटम को RemoteViews ऑब्जेक्ट के तौर पर बनाता है और दिखाता है. शामिल करने के लिए अपने विजेट में कलेक्शन व्यू देखें. RemoteViewsService को लागू करें और RemoteViewsFactory.

RemoteViewsService एक ऐसी सेवा है जिसकी मदद से रिमोट अडैप्टर, RemoteViews ऑब्जेक्ट का अनुरोध कर सकता है. RemoteViewsFactory, कलेक्शन व्यू और उस व्यू के डेटा के बीच के अडैप्टर के लिए इंटरफ़ेस है. जैसे, ListView, GridView, और StackView. StackWidget के सैंपल में, इस सेवा और इंटरफ़ेस को लागू करने के लिए, यहां बॉयलरप्लेट कोड का उदाहरण दिया गया है:

Kotlin

class StackWidgetService : RemoteViewsService() {

    override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
        return StackRemoteViewsFactory(this.applicationContext, intent)
    }
}

class StackRemoteViewsFactory(
        private val context: Context,
        intent: Intent
) : RemoteViewsService.RemoteViewsFactory {

// See the RemoteViewsFactory API reference for the full list of methods to
// implement.

}

Java

public class StackWidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
    }
}

class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

// See the RemoteViewsFactory API reference for the full list of methods to
// implement.

}

सैंपल ऐप्लिकेशन

इस सेक्शन में दिए गए कोड के हिस्से StackWidget से भी लिए गए हैं सैंपल:

पहली इमेज. StackWidget.

इस नमूने में दस व्यू का स्टैक शामिल है, जिसमें शून्य वैल्यू दिखती हैं नौ से नौ. सैंपल विजेट के ये मुख्य व्यवहार हैं:

  • उपयोगकर्ता अगले पेज पर जाने के लिए, विजेट में सबसे ऊपर वाले व्यू को वर्टिकल तरीके से फ़्लिंग कर सकता है या पिछला व्यू. यह एक पहले से मौजूद StackView व्यवहार है.

  • किसी भी उपयोगकर्ता इंटरैक्शन के बिना, विजेट अपने-आप क्रम में देख सकते हैं, जैसे कि स्लाइड शो. ऐसा, res/xml/stackwidgetinfo.xml फ़ाइल में मौजूद android:autoAdvanceViewId="@id/stack_view" सेटिंग की वजह से होता है. यह सेटिंग व्यू आईडी पर लागू होती है. इस मामले में, यह स्टैक व्यू का व्यू आईडी है.

  • अगर उपयोगकर्ता सबसे ऊपर मौजूद व्यू को छूता है, तो विजेट पर Toast मैसेज "छुआ गया व्यू n" दिखता है. यहां n, छूए गए व्यू का इंडेक्स (पोज़िशन) है. यह जानने के लिए कि व्यवहार लागू करने के लिए, 'व्यक्तिगत तौर पर किसी विज्ञापन का व्यवहार जोड़ें' आइटम सेक्शन में मिलेगा.

कलेक्शन के साथ विजेट लागू करना

कलेक्शन के साथ विजेट लागू करने के लिए, किसी भी विजेट को लागू करने का तरीका अपनाएं. इसके बाद, कुछ और चरण पूरे करें: मेनिफ़ेस्ट में बदलाव करें, विजेट लेआउट में कलेक्शन व्यू जोड़ें, और अपने AppWidgetProvider सबक्लास में बदलाव करें.

कलेक्शन वाले विजेट के लिए मेनिफ़ेस्ट

मेनिफ़ेस्ट में विजेट का एलान करें में बताई गई ज़रूरी शर्तों के अलावा, आपको यह भी पक्का करना होगा कि कलेक्शन वाले विजेट, आपके RemoteViewsService से बंधे हों. ऐसा करने के लिए, अपनी मेनिफ़ेस्ट फ़ाइल में अनुमति BIND_REMOTEVIEWS के साथ सेवा का एलान करें. इससे दूसरे ऐप्लिकेशन, आपके विजेट का डेटा आसानी से ऐक्सेस नहीं कर पाते.

उदाहरण के लिए, ऐसा विजेट बनाते समय जो कलेक्शन व्यू को भरने के लिए RemoteViewsService का इस्तेमाल करता है, तो मेनिफ़ेस्ट एंट्री इस तरह दिख सकती है:

<service android:name="MyWidgetService"
    android:permission="android.permission.BIND_REMOTEVIEWS" />

इस उदाहरण में, android:name="MyWidgetService" RemoteViewsService.

कलेक्शन वाले विजेट के लिए लेआउट

विजेट लेआउट की एक्सएमएल फ़ाइल के लिए मुख्य ज़रूरी शर्त यह है कि इसमें इनमें से कोई एक कलेक्शन व्यू शामिल हो: ListView, GridView, StackView या AdapterViewFlipper. यहां StackWidget सैंपल के लिए widget_layout.xml फ़ाइल दी गई है:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <StackView
        android:id="@+id/stack_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:loopViews="true" />
    <TextView
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="@drawable/widget_item_background"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:text="@string/empty_view_text"
        android:textSize="20sp" />
</FrameLayout>

ध्यान दें कि खाली व्यू, उस कलेक्शन व्यू के सिबलिंग होने चाहिए जिसके लिए खाली व्यू, खाली स्थिति दिखाता है.

अपने पूरे विजेट के लिए लेआउट फ़ाइल के अलावा, एक और लेआउट फ़ाइल बनाएं. इससे कलेक्शन में मौजूद हर आइटम के लिए लेआउट तय किया जा सकता है. उदाहरण के लिए, किताबों के कलेक्शन में मौजूद हर किताब के लिए लेआउट. StackWidget सैंपल में ये शामिल हैं सिर्फ़ एक आइटम लेआउट फ़ाइल, widget_item.xml, क्योंकि सभी आइटम एक ही का इस्तेमाल करते हैं लेआउट.

संग्रह वाले विजेट के लिए AppWidgetProvider क्लास

सामान्य विजेट की तरह ही, आपके AppWidgetProvider सबक्लास में मौजूद ज़्यादातर कोड, आम तौर पर onUpdate() में जाता है. नई रिपोर्ट बनाते समय onUpdate() को लागू करने की प्रक्रिया में बड़ा अंतर कलेक्शन वाला विजेट है, तो आपको कॉल setRemoteAdapter(). इससे कलेक्शन व्यू को पता चलता है कि उसे डेटा कहां से चाहिए. इसके बाद, RemoteViewsService आपके लागू किए गए RemoteViewsFactory को दिखा सकता है और विजेट सही डेटा दिखा सकता है. इस तरीके को कॉल करते समय, ऐसा इंटेंट पास करें जो RemoteViewsService को लागू करने के बारे में बताता हो. साथ ही, उस विजेट का आईडी भी पास करें जिसे अपडेट करना है.

उदाहरण के लिए, यहां बताया गया है कि StackWidget सैंपल, onUpdate() को विजेट कलेक्शन के लिए रिमोट अडैप्टर के तौर पर सेट करने के लिए, onUpdate() कॉलबैक का तरीका कैसे लागू करता है:

Kotlin

override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray
) {
    // Update each of the widgets with the remote adapter.
    appWidgetIds.forEach { appWidgetId ->

        // Set up the intent that starts the StackViewService, which
        // provides the views for this collection.
        val intent = Intent(context, StackWidgetService::class.java).apply {
            // Add the widget ID to the intent extras.
            putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
            data = Uri.parse(toUri(Intent.URI_INTENT_SCHEME))
        }
        // Instantiate the RemoteViews object for the widget layout.
        val views = RemoteViews(context.packageName, R.layout.widget_layout).apply {
            // Set up the RemoteViews object to use a RemoteViews adapter.
            // This adapter connects to a RemoteViewsService through the
            // specified intent.
            // This is how you populate the data.
            setRemoteAdapter(R.id.stack_view, intent)

            // The empty view is displayed when the collection has no items.
            // It must be in the same layout used to instantiate the
            // RemoteViews object.
            setEmptyView(R.id.stack_view, R.id.empty_view)
        }

        // Do additional processing specific to this widget.

        appWidgetManager.updateAppWidget(appWidgetId, views)
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds)
}

Java

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // Update each of the widgets with the remote adapter.
    for (int i = 0; i < appWidgetIds.length; ++i) {

        // Set up the intent that starts the StackViewService, which
        // provides the views for this collection.
        Intent intent = new Intent(context, StackWidgetService.class);
        // Add the widget ID to the intent extras.
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        // Instantiate the RemoteViews object for the widget layout.
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        // Set up the RemoteViews object to use a RemoteViews adapter.
        // This adapter connects to a RemoteViewsService through the specified
        // intent.
        // This is how you populate the data.
        views.setRemoteAdapter(R.id.stack_view, intent);

        // The empty view is displayed when the collection has no items.
        // It must be in the same layout used to instantiate the RemoteViews
        // object.
        views.setEmptyView(R.id.stack_view, R.id.empty_view);

        // Do additional processing specific to this widget.

        appWidgetManager.updateAppWidget(appWidgetIds[i], views);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

डेटा सेव रखना

जैसा कि इस पेज पर बताया गया है, RemoteViewsService सब-क्लास RemoteViewsFactory का इस्तेमाल, रिमोट कलेक्शन व्यू में अपने-आप जानकारी भरने के लिए किया जाता है.

खास तौर पर, यह तरीका अपनाएं:

  1. सब-क्लास RemoteViewsService. RemoteViewsService के ज़रिए सेवा देने वाली कंपनी जिसके लिए रिमोट अडैप्टर, RemoteViews का अनुरोध कर सकता है.

  2. अपनी RemoteViewsService सबक्लास में, ऐसी क्लास शामिल करें जो RemoteViewsFactory इंटरफ़ेस को लागू करती हो. RemoteViewsFactory, रिमोट कलेक्शन व्यू और उस व्यू के डेटा के बीच अडैप्टर के लिए इंटरफ़ेस है. जैसे, ListView, GridView, StackView. डेटासेट में मौजूद हर आइटम के लिए, RemoteViews ऑब्जेक्ट बनाने की ज़िम्मेदारी आपके पास होती है. यह इंटरफ़ेस, Adapter के आस-पास एक पतला रैपर है.

अपनी सेवा के किसी एक इंस्टेंस या उसमें मौजूद डेटा पर भरोसा करके, उसे बनाए रखने के लिए काम नहीं किया जा सकता. अपने RemoteViewsService में तब तक डेटा सेव न करें, जब तक वह स्टैटिक न हो. अगर आपको अपने विजेट का डेटा सेव रखना है, तो सबसे अच्छा तरीका यह है कि आप ऐसे ContentProvider का इस्तेमाल करें जिसका डेटा प्रोसेस के लाइफ़साइकल के बाद भी सेव रहता है. उदाहरण के लिए, किराने की दुकान का विजेट, किराने की सूची के हर आइटम की स्थिति को हमेशा मौजूद रहने वाली जगह पर सेव कर सकता है. जैसे, SQL डेटाबेस.

RemoteViewsService को लागू करने का मुख्य कॉन्टेंट यह है RemoteViewsFactory की जानकारी नीचे दिए गए सेक्शन में दी गई है.

RemoteViewsफ़ैक्ट्री इंटरफ़ेस

RemoteViewsFactory इंटरफ़ेस को लागू करने वाली आपकी कस्टम क्लास, विजेट को अपने कलेक्शन में मौजूद आइटम का डेटा उपलब्ध कराती है. ऐसा करने के लिए, यह आपके विजेट आइटम की एक्सएमएल लेआउट फ़ाइल को डेटा के सोर्स के साथ जोड़ता है. डेटा का यह सोर्स, डेटाबेस से लेकर किसी साधारण कलेक्शन तक कुछ भी हो सकता है. StackWidget सैंपल में, डेटा सोर्स WidgetItems का ऐरे है. RemoteViewsFactory, डेटा को रिमोट कलेक्शन व्यू में जोड़ने के लिए अडैप्टर के तौर पर काम करता है.

अपने विज्ञापनों के लिए, RemoteViewsFactory सब-क्लास ये हैं onCreate() और getViewAt().

पहली बार फ़ैक्ट्री बनाते समय, सिस्टम onCreate() को कॉल करता है. यहां अपने डेटा सोर्स के लिए, कोई कनेक्शन या कर्सर सेट अप किया जाता है. उदाहरण के लिए, StackWidget सैंपल, WidgetItem ऑब्जेक्ट के कलेक्शन को शुरू करने के लिए onCreate() का इस्तेमाल करता है. जब आपका विजेट चालू होता है, तो सिस्टम इन ऑब्जेक्ट को ऐरे में मौजूद उनकी इंडेक्स पोज़िशन का इस्तेमाल करके ऐक्सेस करता है और उनमें मौजूद टेक्स्ट दिखाता है.

यहां StackWidget सैंपल के RemoteViewsFactory का एक छोटा हिस्सा दिया गया है इस तरह लागू किया जाता है, जो onCreate() तरीके के हिस्से दिखाता है:

Kotlin

private const val REMOTE_VIEW_COUNT: Int = 10

class StackRemoteViewsFactory(
        private val context: Context
) : RemoteViewsService.RemoteViewsFactory {

    private lateinit var widgetItems: List<WidgetItem>

    override fun onCreate() {
        // In onCreate(), set up any connections or cursors to your data
        // source. Heavy lifting, such as downloading or creating content,
        // must be deferred to onDataSetChanged() or getViewAt(). Taking
        // more than 20 seconds on this call results in an ANR.
        widgetItems = List(REMOTE_VIEW_COUNT) { index -> WidgetItem("$index!") }
        ...
    }
    ...
}

Java

class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
    private static final int REMOTE_VIEW_COUNT = 10;
    private List<WidgetItem> widgetItems = new ArrayList<WidgetItem>();

    public void onCreate() {
        // In onCreate(), setup any connections or cursors to your data
        // source. Heavy lifting, such as downloading or creating content,
        // must be deferred to onDataSetChanged() or getViewAt(). Taking
        // more than 20 seconds on this call results in an ANR.
        for (int i = 0; i < REMOTE_VIEW_COUNT; i++) {
            widgetItems.add(new WidgetItem(i + "!"));
        }
        ...
    }
...

RemoteViewsFactory का तरीका getViewAt(), डेटा सेट में बताए गए position पर मौजूद डेटा से जुड़ा RemoteViews ऑब्जेक्ट दिखाता है. यहां StackWidget सैंपल के RemoteViewsFactory लागू करने का एक उदाहरण दिया गया है:

Kotlin

override fun getViewAt(position: Int): RemoteViews {
    // Construct a remote views item based on the widget item XML file
    // and set the text based on the position.
    return RemoteViews(context.packageName, R.layout.widget_item).apply {
        setTextViewText(R.id.widget_item, widgetItems[position].text)
    }
}

Java

public RemoteViews getViewAt(int position) {
    // Construct a remote views item based on the widget item XML file
    // and set the text based on the position.
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_item);
    views.setTextViewText(R.id.widget_item, widgetItems.get(position).text);
    return views;
}

अलग-अलग आइटम के लिए व्यवहार जोड़ना

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

जैसा कि onUpdate() के साथ इवेंट मैनेज करना' सेक्शन में बताया गया है क्लास, जिसे आम तौर पर इस्तेमाल किया जाता है setOnClickPendingIntent(): Activity को लॉन्च करने के लिए बटन दबाएँ. लेकिन किसी कलेक्शन आइटम में चाइल्ड व्यू के लिए इस तरीके की अनुमति नहीं है. उदाहरण के लिए, ग्लोबल बटन सेट अप करने के लिए, setOnClickPendingIntent() का इस्तेमाल किया जा सकता है उदाहरण के लिए, ऐप्लिकेशन को लॉन्च करने वाले Gmail विजेट में, लेकिन अलग-अलग सूची आइटम.

इसके बजाय, किसी कलेक्शन के अलग-अलग आइटम पर क्लिक करने की प्रोसेस जोड़ने के लिए, setOnClickFillInIntent(). इसके लिए, इंटेंट टेंप्लेट को सेट अप करना होगा और फिर इसमें हर आइटम के लिए एक फ़िल-इन इंटेंट सेट करें आपके RemoteViewsFactory के ज़रिए संग्रह.

इस सेक्शन में, StackWidget सैंपल का इस्तेमाल करके यह बताया जाता है कि व्यवहार को कैसे जोड़ा जाए अलग-अलग आइटम. StackWidget सैंपल में, अगर उपयोगकर्ता सबसे ऊपर मौजूद व्यू को छूता है, तो विजेट Toast मैसेज "छुआ गया व्यू n" दिखाता है. यहां n, छूए गए व्यू का इंडेक्स (पोज़िशन) है. आइए जानते हैं कि ऐसा कैसे करें:

  • StackWidgetProvider—एक AppWidgetProvider सब-क्लास—एक कस्टम कार्रवाई के साथ एक लंबित इंटेंट बनाता है, जिसे TOAST_ACTION.

  • जब उपयोगकर्ता किसी व्यू को छूता है, तो इंटेंट ट्रिगर हो जाता है और ब्रॉडकास्ट होता है TOAST_ACTION.

  • इस ब्रॉडकास्ट को StackWidgetProvider क्लास onReceive() तरीका और विजेट Toast मैसेज दिखाता है स्पर्श करें. कलेक्शन आइटम का डेटा, RemoteViewsService के ज़रिए RemoteViewsFactory से मिलता है.

पेंडिंग इंटेंट टेंप्लेट सेट अप करना

StackWidgetProvider (AppWidgetProvider सबक्लास), PendingIntent सेट अप करता है. संग्रह के अलग-अलग आइटम उनका सेट अप नहीं कर सकते लंबित इंटेंट मौजूद हैं. इसके बजाय, पूरा कलेक्शन एक ऐसा टेंप्लेट सेट अप करता है जिसमें कार्रवाई पूरी नहीं हुई है. साथ ही, हर आइटम के लिए, अलग-अलग व्यवहार बनाने के लिए, आइटम में जानकारी भरने की सुविधा सेट अप की जाती है.

इस कक्षा को वह प्रसारण भी प्राप्त होता है जिसे उपयोगकर्ता द्वारा व्यू. यह इस इवेंट को अपने onReceive() मेथड में प्रोसेस करता है. अगर इंटेंट की कार्रवाई TOAST_ACTION है, तो विजेट मौजूदा व्यू के लिए Toast मैसेज दिखाता है.

Kotlin

const val TOAST_ACTION = "com.example.android.stackwidget.TOAST_ACTION"
const val EXTRA_ITEM = "com.example.android.stackwidget.EXTRA_ITEM"

class StackWidgetProvider : AppWidgetProvider() {

    ...

    // Called when the BroadcastReceiver receives an Intent broadcast.
    // Checks whether the intent's action is TOAST_ACTION. If it is, the
    // widget displays a Toast message for the current item.
    override fun onReceive(context: Context, intent: Intent) {
        val mgr: AppWidgetManager = AppWidgetManager.getInstance(context)
        if (intent.action == TOAST_ACTION) {
            val appWidgetId: Int = intent.getIntExtra(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID
            )
            // EXTRA_ITEM represents a custom value provided by the Intent
            // passed to the setOnClickFillInIntent() method to indicate the
            // position of the clicked item. See StackRemoteViewsFactory in
            // Set the fill-in Intent for details.
            val viewIndex: Int = intent.getIntExtra(EXTRA_ITEM, 0)
            Toast.makeText(context, "Touched view $viewIndex", Toast.LENGTH_SHORT).show()
        }
        super.onReceive(context, intent)
    }

    override fun onUpdate(
            context: Context,
            appWidgetManager: AppWidgetManager,
            appWidgetIds: IntArray
    ) {
        // Update each of the widgets with the remote adapter.
        appWidgetIds.forEach { appWidgetId ->

            // Sets up the intent that points to the StackViewService that
            // provides the views for this collection.
            val intent = Intent(context, StackWidgetService::class.java).apply {
                putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
                // When intents are compared, the extras are ignored, so embed
                // the extra sinto the data so that the extras are not ignored.
                data = Uri.parse(toUri(Intent.URI_INTENT_SCHEME))
            }
            val rv = RemoteViews(context.packageName, R.layout.widget_layout).apply {
                setRemoteAdapter(R.id.stack_view, intent)

                // The empty view is displayed when the collection has no items.
                // It must be a sibling of the collection view.
                setEmptyView(R.id.stack_view, R.id.empty_view)
            }

            // This section makes it possible for items to have individualized
            // behavior. It does this by setting up a pending intent template.
            // Individuals items of a collection can't set up their own pending
            // intents. Instead, the collection as a whole sets up a pending
            // intent template, and the individual items set a fillInIntent
            // to create unique behavior on an item-by-item basis.
            val toastPendingIntent: PendingIntent = Intent(
                    context,
                    StackWidgetProvider::class.java
            ).run {
                // Set the action for the intent.
                // When the user touches a particular view, it has the effect of
                // broadcasting TOAST_ACTION.
                action = TOAST_ACTION
                putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
                data = Uri.parse(toUri(Intent.URI_INTENT_SCHEME))

                PendingIntent.getBroadcast(context, 0, this, PendingIntent.FLAG_UPDATE_CURRENT)
            }
            rv.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent)

            appWidgetManager.updateAppWidget(appWidgetId, rv)
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds)
    }
}

Java

public class StackWidgetProvider extends AppWidgetProvider {
    public static final String TOAST_ACTION = "com.example.android.stackwidget.TOAST_ACTION";
    public static final String EXTRA_ITEM = "com.example.android.stackwidget.EXTRA_ITEM";

    ...

    // Called when the BroadcastReceiver receives an Intent broadcast.
    // Checks whether the intent's action is TOAST_ACTION. If it is, the
    // widget displays a Toast message for the current item.
    @Override
    public void onReceive(Context context, Intent intent) {
        AppWidgetManager mgr = AppWidgetManager.getInstance(context);
        if (intent.getAction().equals(TOAST_ACTION)) {
            int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
            // EXTRA_ITEM represents a custom value provided by the Intent
            // passed to the setOnClickFillInIntent() method to indicate the
            // position of the clicked item. See StackRemoteViewsFactory in
            // Set the fill-in Intent for details.
            int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0);
            Toast.makeText(context, "Touched view " + viewIndex, Toast.LENGTH_SHORT).show();
        }
        super.onReceive(context, intent);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // Update each of the widgets with the remote adapter.
        for (int i = 0; i < appWidgetIds.length; ++i) {

            // Sets up the intent that points to the StackViewService that
            // provides the views for this collection.
            Intent intent = new Intent(context, StackWidgetService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            // When intents are compared, the extras are ignored, so embed
            // the extras into the data so that the extras are not
            // ignored.
            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            rv.setRemoteAdapter(appWidgetIds[i], R.id.stack_view, intent);

            // The empty view is displayed when the collection has no items. It
            // must be a sibling of the collection view.
            rv.setEmptyView(R.id.stack_view, R.id.empty_view);

            // This section makes it possible for items to have individualized
            // behavior. It does this by setting up a pending intent template.
            // Individuals items of a collection can't set up their own pending
            // intents. Instead, the collection as a whole sets up a pending
            // intent template, and the individual items set a fillInIntent
            // to create unique behavior on an item-by-item basis.
            Intent toastIntent = new Intent(context, StackWidgetProvider.class);
            // Set the action for the intent.
            // When the user touches a particular view, it has the effect of
            // broadcasting TOAST_ACTION.
            toastIntent.setAction(StackWidgetProvider.TOAST_ACTION);
            toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
            rv.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);

            appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
}

भरने का इंटेंट सेट करें

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

Kotlin

private const val REMOTE_VIEW_COUNT: Int = 10

class StackRemoteViewsFactory(
        private val context: Context,
        intent: Intent
) : RemoteViewsService.RemoteViewsFactory {

    private lateinit var widgetItems: List<WidgetItem>
    private val appWidgetId: Int = intent.getIntExtra(
            AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID
    )

    override fun onCreate() {
        // In onCreate(), set up any connections or cursors to your data source.
        // Heavy lifting, such as downloading or creating content, must be
        // deferred to onDataSetChanged() or getViewAt(). Taking more than 20
        // seconds on this call results in an ANR.
        widgetItems = List(REMOTE_VIEW_COUNT) { index -> WidgetItem("$index!") }
        ...
    }
    ...

    override fun getViewAt(position: Int): RemoteViews {
        // Construct a remote views item based on the widget item XML file
        // and set the text based on the position.
        return RemoteViews(context.packageName, R.layout.widget_item).apply {
            setTextViewText(R.id.widget_item, widgetItems[position].text)

            // Set a fill-intent to fill in the pending intent template.
            // that is set on the collection view in StackWidgetProvider.
            val fillInIntent = Intent().apply {
                Bundle().also { extras ->
                    extras.putInt(EXTRA_ITEM, position)
                    putExtras(extras)
                }
            }
            // Make it possible to distinguish the individual on-click
            // action of a given item.
            setOnClickFillInIntent(R.id.widget_item, fillInIntent)
            ...
        }
    }
    ...
}

Java

public class StackWidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
    }
}

class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
    private static final int count = 10;
    private List<WidgetItem> widgetItems = new ArrayList<WidgetItem>();
    private Context context;
    private int appWidgetId;

    public StackRemoteViewsFactory(Context context, Intent intent) {
        this.context = context;
        appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    // Initialize the data set.
    public void onCreate() {
        // In onCreate(), set up any connections or cursors to your data
        // source. Heavy lifting, such as downloading or creating
        // content, must be deferred to onDataSetChanged() or
        // getViewAt(). Taking more than 20 seconds on this call results
        // in an ANR.
        for (int i = 0; i < count; i++) {
            widgetItems.add(new WidgetItem(i + "!"));
        }
        ...
    }

    // Given the position (index) of a WidgetItem in the array, use the
    // item's text value in combination with the widget item XML file to
    // construct a RemoteViews object.
    public RemoteViews getViewAt(int position) {
        // Position always ranges from 0 to getCount() - 1.

        // Construct a RemoteViews item based on the widget item XML
        // file and set the text based on the position.
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_item);
        rv.setTextViewText(R.id.widget_item, widgetItems.get(position).text);

        // Set a fill-intent to fill in the pending
        // intent template that is set on the collection view in
        // StackWidgetProvider.
        Bundle extras = new Bundle();
        extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
        Intent fillInIntent = new Intent();
        fillInIntent.putExtras(extras);
        // Make it possible to distinguish the individual on-click
        // action of a given item.
        rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);

        // Return the RemoteViews object.
        return rv;
    }
    ...
}

कलेक्शन का डेटा अप-टू-डेट रखना

दूसरे चित्र में, कलेक्शन का इस्तेमाल करने वाले विजेट में अपडेट फ़्लो को दिखाया गया है. यह दिखाता है विजेट कोड, RemoteViewsFactory के साथ कैसे इंटरैक्ट करता है और यह कैसे किया जा सकता है अपडेट ट्रिगर करें:

दूसरी इमेज. अपडेट के दौरान RemoteViewsFactory के साथ इंटरैक्शन.

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

ऐसा करने के लिए, notifyAppWidgetViewDataChanged() को कॉल करने के लिए, AppWidgetManager का इस्तेमाल करें. इस कॉल के नतीजे में आपके RemoteViewsFactory ऑब्जेक्ट के onDataSetChanged() तरीका है, जिसकी मदद से किसी भी तरह का नया डेटा फ़ेच किया जा सकता है.

आप onDataSetChanged() कॉलबैक. आपको गारंटी है कि यह कॉल पूरा हो जाएगा RemoteViewsFactory से मेटाडेटा या व्यू डेटा फ़ेच करने से पहले. आपने लोगों तक पहुंचाया मुफ़्त में getViewAt() में प्रोसेसिंग-इंटेंसिव ऑपरेशन भी कर सकता है तरीका. अगर इस कॉल में ज़्यादा समय लगता है, तो RemoteViewsFactory ऑब्जेक्ट के getLoadingView() तरीके से तय किए गए लोडिंग व्यू को, कलेक्शन व्यू की उसी जगह पर तब तक दिखाया जाता है, जब तक वह नतीजे नहीं दिखा देता.

किसी कलेक्शन को सीधे पास करने के लिए, RemoteCollectionItem का इस्तेमाल करें

Android 12 (एपीआई लेवल 31) ने setRemoteAdapter(int viewId, RemoteViews.RemoteCollectionItems items) को जोड़ा तरीका, जिससे आपका ऐप्लिकेशन किसी कलेक्शन व्यू. अगर आपने अडैप्टर को इस तरीके से सेट किया है, तो आपको ये काम करने की ज़रूरत नहीं है RemoteViewsFactory लागू करें. आपको कॉल करने की ज़रूरत नहीं है notifyAppWidgetViewDataChanged().

आपके अडैप्टर को पॉप्युलेट करना आसान बनाने के अलावा, इस तरीके को इससे उपयोगकर्ताओं को सूची में नीचे की ओर स्क्रोल करने पर, नए आइटम अपने-आप भरने में लगने वाला समय कम हो जाता है नया आइटम दिखाने के लिए. अडैप्टर सेट करने के लिए, इस तरीके का इस्तेमाल तब तक किया जा सकता है, जब तक आपके कलेक्शन आइटम का सेट अपेक्षाकृत छोटा हो. हालांकि, उदाहरण के लिए, अगर आपके कलेक्शन में Bitmaps को setImageViewBitmap में पास किए जाने वाले कई Bitmaps हैं, तो यह तरीका ठीक से काम नहीं करता.

अगर कलेक्शन में लेआउट के एक तय सेट का इस्तेमाल नहीं किया जाता है, यानी कुछ आइटम कभी-कभी ही मौजूद होते हैं, तो setViewTypeCount का इस्तेमाल करके बताएं कि कलेक्शन में ज़्यादा से ज़्यादा कितने यूनीक लेआउट हो सकते हैं. इससे, आपके ऐप्लिकेशन विजेट के अपडेट में अडैप्टर का फिर से इस्तेमाल किया जा सकता है.

यहां आसान बनाए गए RemoteViews कलेक्शन को लागू करने का उदाहरण दिया गया है.

Kotlin

val itemLayouts = listOf(
        R.layout.item_type_1,
        R.layout.item_type_2,
        ...
)

remoteView.setRemoteAdapter(
        R.id.list_view,
        RemoteViews.RemoteCollectionItems.Builder()
            .addItem(/* id= */ ID_1, RemoteViews(context.packageName, R.layout.item_type_1))
            .addItem(/* id= */ ID_2, RemoteViews(context.packageName, R.layout.item_type_2))
            ...
            .setViewTypeCount(itemLayouts.count())
            .build()
)

Java

List<Integer> itemLayouts = Arrays.asList(
    R.layout.item_type_1,
    R.layout.item_type_2,
    ...
);

remoteView.setRemoteAdapter(
    R.id.list_view,
    new RemoteViews.RemoteCollectionItems.Builder()
        .addItem(/* id= */ ID_1, new RemoteViews(context.getPackageName(), R.layout.item_type_1))
        .addItem(/* id= */ ID_2, new RemoteViews(context.getPackageName(), R.layout.item_type_2))
        ...
        .setViewTypeCount(itemLayouts.size())
        .build()
);