Glance の相互運用性

場合によっては、XML と RemoteViews を使用してビューを提供することがあります。 Glance を使用せずに機能を実装済みの場合や、現在の Glance API ではその機能がまだ利用できない場合、または実装できない場合などです。このような状況に対応するため、Glance には相互運用性 API である AndroidRemoteViews が用意されています。

AndroidRemoteViews コンポーザブルを使用すると、RemoteViews を他のコンポーザブルと一緒に配置できます。

val packageName = LocalContext.current.packageName
Column(modifier = GlanceModifier.fillMaxSize()) {
    Text("Isn't that cool?")
    AndroidRemoteViews(RemoteViews(packageName, R.layout.example_layout))
}

Glance を使用しない場合と同様に RemoteViews を作成して定義し、パラメータとして渡します。

また、コンポーザブルの RemoteViews コンテナを作成することもできます。

AndroidRemoteViews(
    remoteViews = RemoteViews(packageName, R.layout.my_container_view),
    containerViewId = R.id.example_view
) {
    Column(modifier = GlanceModifier.fillMaxSize()) {
        Text("My title")
        Text("Maybe a long content...")
    }
}

この場合、「container」を含むレイアウトが定義された ID で渡されます。このコンテナは、定義されたコンテンツを配置するために使用されるため、ViewGroupである必要があります。