컬렉션 위젯 사용

컬렉션 위젯은 동일한 유형의 여러 요소를 표시하는 데 특화되어 있습니다(예: 갤러리 앱의 사진, 뉴스 앱의 기사 또는 메시지를 보낼 수 있습니다 컬렉션 위젯은 일반적으로 두 가지 용도에 사례: 컬렉션을 탐색하고 컬렉션의 요소를 자세히 살펴보겠습니다. 컬렉션 위젯은 세로로 스크롤할 수 있습니다.

이러한 위젯은 표시할 항목 RemoteViewsService개 원격 데이터(예: provider 위젯은 컬렉션이라고 하는 다음 보기 유형 중 하나를 사용하여 데이터를 가져올 수 있습니다. 조회수:

ListView
목록을 세로로 스크롤하는 것이 좋습니다
GridView
2차원 스크롤 그리드를 표시합니다
StackView
사용자가 전면 카드를 위/아래로 돌려 이전/다음 카드를 볼 수 있는 스택형 카드 뷰(예: rolodex).
AdapterViewFlipper
어댑터 지원 단순 애니메이션 ViewAnimator 있습니다. 한 번에 한 자녀만 표시됩니다.

이러한 컬렉션 뷰는 원격 데이터로 지원되는 컬렉션을 표시하기 때문에 Adapter를 사용하여 사용자를 바인딩 인터페이스를 제공합니다 Adapter는 데이터 세트의 개별 항목을 결합합니다. 개별 View 객체에 추가합니다.

이러한 컬렉션 뷰는 어댑터에 의해 지원되므로 Android 프레임워크는 위젯에서 사용할 수 있도록 추가 아키텍처를 포함해야 합니다. 위젯의 컨텍스트에서 AdapterAdapter 인터페이스를 감싸는 씬 래퍼인 RemoteViewsFactory로 대체됩니다. 컬렉션의 특정 항목이 반환되면 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.

}

자바

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 샘플:

그림 1. StackWidget입니다.

이 샘플은 0~9 값을 표시하는 뷰 10개의 스택으로 구성됩니다. 샘플 위젯에는 다음과 같은 기본 동작이 있습니다.

  • 사용자는 위젯의 상단 뷰를 세로로 흔들어 다음 또는 이전 뷰를 표시할 수 있습니다. 이는 내장된 StackView 동작입니다.

  • 위젯은 사용자 상호작용 없이 슬라이드쇼와 같이 뷰 사이를 자동으로 순차적으로 이동합니다. 이는 android:autoAdvanceViewId="@id/stack_view" res/xml/stackwidgetinfo.xml 파일. 이 설정은 보기 ID, 이 경우 스택 뷰의 뷰 ID입니다.

  • 사용자가 상단 뷰를 터치하면 위젯에 Toast 메시지 "터치한 보기 n" 각 항목의 의미는 다음과 같습니다. n은 터치된 뷰의 색인 (위치)입니다. 동작을 구현하는 방법에 관한 자세한 내용은 개별 항목에 동작 추가 섹션을 참고하세요.

컬렉션이 포함된 위젯 구현

컬렉션이 포함된 위젯을 구현하려면 아래 절차를 따라 하나의 위젯으로 이동한 후 몇 가지 추가 단계를 수행합니다. 컬렉션 뷰를 위젯 레이아웃에 추가하며, AppWidgetProvider 서브클래스.

컬렉션이 있는 위젯의 매니페스트

매니페스트에서 위젯 선언에 나열된 요구사항 외에도 컬렉션이 포함된 위젯이 RemoteViewsService에 바인딩할 수 있도록 해야 합니다. 매니페스트 파일에서 BIND_REMOTEVIEWS 권한으로 서비스를 선언하면 됩니다. 이렇게 하면 다른 애플리케이션이 위젯의 데이터에 자유롭게 액세스할 수 없습니다.

예를 들어 RemoteViewsService를 사용하여 컬렉션 뷰에서 매니페스트 항목은 다음과 같을 수 있습니다.

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

이 예에서 android:name="MyWidgetService"RemoteViewsService의 서브클래스를 나타냅니다.

컬렉션이 포함된 위젯의 레이아웃

위젯 레이아웃 XML 파일의 기본 요구사항은 컬렉션 뷰: 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 및 업데이트할 위젯을 지정하는 위젯 ID입니다.

예를 들어 StackWidget 샘플이 onUpdate()를 구현하는 방법은 다음과 같습니다. 콜백 메서드를 사용하여 RemoteViewsService를 애플리케이션의 원격 어댑터로 설정합니다. 위젯 컬렉션:

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)
}

자바

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(다음 섹션에 설명되어 있음)

RemoteViewsFactory 인터페이스

RemoteViewsFactory 인터페이스를 구현하는 맞춤 클래스는 다음을 제공합니다. 해당 컬렉션의 항목 데이터가 포함된 위젯입니다. 이렇게 하기 위해 맞춤 클래스는 위젯 항목 XML 레이아웃 파일을 데이터 소스와 결합합니다. 데이터베이스에서 단일 배열에 이르기까지 모든 항목이 데이터 소스가 될 수 있습니다. StackWidget에 있음 샘플의 데이터 소스는 WidgetItems의 배열입니다. RemoteViewsFactory 데이터를 원격 컬렉션 뷰에 결합하는 어댑터로 작동합니다.

RemoteViewsFactory 서브클래스를 위해 구현해야 하는 가장 중요한 두 가지 메서드는 onCreate()getViewAt()입니다.

처음으로 팩토리를 만들 때 시스템에서는 onCreate()을 호출합니다. 여기에서 데이터 소스의 연결 또는 커서를 설정합니다. 예를 들어 StackWidget 샘플은 onCreate()를 사용하여 WidgetItem 객체 배열을 초기화합니다. 위젯이 활성 상태이면 시스템은 객체를 사용하여 배열에서 색인 위치를 사용하여 합니다.

다음은 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!") }
        ...
    }
    ...
}

자바

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)
    }
}

자바

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 샘플에서 사용자가 상단 뷰를 터치하면 위젯에 '터치된 뷰 n' Toast 메시지가 표시됩니다. 여기서 n은 터치된 뷰의 색인 (위치)입니다. 내용은 다음과 같습니다.

  • AppWidgetProvider 서브클래스인 StackWidgetProviderTOAST_ACTION라는 맞춤 작업이 있는 대기중 인텐트를 만듭니다.

  • 사용자가 뷰를 터치하면 인텐트가 실행되고 브로드캐스트됩니다. TOAST_ACTION

  • 이 브로드캐스트는 StackWidgetProvider 클래스의 onReceive() 메서드를 호출하고 위젯에 Toast 메시지가 표시됨 볼 수 있습니다. 컬렉션 항목의 데이터는 RemoteViewsFactory에서 RemoteViewsService를 통해 제공합니다.

대기 중인 인텐트 템플릿 설정

StackWidgetProvider(AppWidgetProvider 서브클래스)는 대기중 인텐트를 설정합니다. 컬렉션의 개별 항목은 사용할 수 있습니다 대신 컬렉션 전체가 대기 중인 인텐트를 설정합니다. 템플릿, 개별 항목은 채우기 인텐트를 설정하여 고유한 항목별로 다르게 작동합니다.

이 클래스는 사용자가 뷰를 터치할 때 전송되는 브로드캐스트도 수신합니다. 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)
    }
}

자바

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)
            ...
        }
    }
    ...
}

자바

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;
    }
    ...
}

컬렉션 데이터 최신 상태 유지

그림 2는 컬렉션을 사용하는 위젯의 업데이트 흐름을 보여줍니다. 위젯 코드가 RemoteViewsFactory와 상호작용하는 방식, 업데이트를 트리거할 수 있는 방법을 보여줍니다.

그림 2. 업데이트 중 RemoteViewsFactory와의 상호작용

컬렉션을 사용하는 위젯은 사용자에게 최신 콘텐츠를 제공할 수 있습니다. 대상 예를 들어 Gmail 위젯은 사용자에게 받은편지함의 스냅샷을 제공합니다. 이렇게 하려면 RemoteViewsFactory 및 컬렉션 뷰를 트리거하여 새 데이터를 표시합니다.

이렇게 하려면 호출하려면 AppWidgetManager notifyAppWidgetViewDataChanged() 이 호출을 실행하면 RemoteViewsFactory 객체의 onDataSetChanged() 메서드에 콜백이 전송되어 새 데이터를 가져올 수 있습니다.

처리 집약적인 작업을 onDataSetChanged() 콜백입니다. 이 통화는 반드시 완료됩니다. RemoteViewsFactory에서 메타데이터 또는 뷰 데이터를 가져오기 전. 나 getViewAt() 내에서 처리 집약적인 작업을 실행할 수도 있습니다. 메서드를 사용하여 축소하도록 요청합니다. 이 호출이 오래 걸리는 경우 RemoteViewsFactory 객체의 getLoadingView() 메서드 - 컬렉션 뷰의 해당 위치에 표시됩니다. 반환됩니다.

RemoteCollectionItems를 사용하여 컬렉션을 직접 전달

Android 12 (API 수준 31)는 setRemoteAdapter(int viewId, RemoteViews.RemoteCollectionItems items)를 추가합니다. 메서드를 사용하면 객체를 채울 때 앱에서 컬렉션을 직접 전달할 수 있습니다. 컬렉션 뷰 이 방법을 사용하여 어댑터를 설정하면 RemoteViewsFactory를 구현하고 notifyAppWidgetViewDataChanged()

이 접근 방식은 어댑터를 더 쉽게 채울 수 있을 뿐만 아니라 사용자가 목록을 아래로 스크롤하여 새 항목을 표시할 때 새 항목을 채우는 데 걸리는 지연 시간도 제거합니다. 컬렉션 항목 세트가 비교적 작은 경우 어댑터를 설정하는 이 접근 방식이 좋습니다. 그러나 예를 들어 컬렉션에 포함된 여러 개의 Bitmaps가 있는 경우에는 적합하지 않습니다. setImageViewBitmap에 전달되었습니다.

컬렉션이 일정한 레이아웃 세트를 사용하지 않으면(즉, 일부 항목이 간혹 있는 경우) 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()
)

자바

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()
);