検索コンテンツの UI ビュー ジェネレータをリクエストする
SDK で検索コンテンツの UI ビューを返す機能が追加されました。
Search Content は、複数の種類のコンテンツ
機能を指す一般的な用語です。各種類のコンテンツ
機能をリクエストする方法については、以下のセクションをご覧ください。
検索の繰り返し
検索の繰り返しの UI ビューを取得するには、まず次の手順で検索コンテンツ ジェネレータを取得する必要があります。
GetSearchContentViewGeneratorCallbackクラスのonSuccessメソッドとonErrorメソッドをオーバーライドします。GetSearchContentViewGeneratorCallbackクラスのインスタンスを作成します。GetSearchContentViewOptionsクラスのインスタンスを作成し、setSearchRepeatContextメソッドを呼び出して検索の繰り返しコンテキストを設定します。(省略可)このオブジェクトはSearchContentViewOptionsオブジェクトも受け取ります。このオブジェクトには、UI の外観をカスタマイズするためのオプションが用意されています。SearchInAppsServiceのgetSearchContentView(GetSearchContentViewOptions, GetSearchContentViewGeneratorCallback)関数を呼び出します。- UI ジェネレータを取得したら、ViewModel に保存することを検討してください。これにより、アクティビティの再作成が必要になった場合(アプリの実行中に構成が変更された場合など)に、ジェネレータを再度リクエストする必要がなくなります。
サンプルコード
Java
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchContentViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetSearchContentViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchContentViewGenerator;
...
public class MainActivity extends AppCompatActivity {
private SearchInAppsService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
service = SearchInAppsService.create(this);
GetSearchContentViewGeneratorCallback searchContentCallback =
new GetSearchContentViewGeneratorCallback() {
@Override
public void onSuccess(SearchContentViewGenerator generator) {
...
}
@Override
public void onError(String errorMessage) {
...
}
};
// Uses the default SearchContentViewOptions.
service.getSearchContentView(
new GetSearchContentViewOptions().setSearchRepeatContext(
"sample search repeat"), searchContentCallback);
...
}
@Override
public void onDestroy() {
service.shutDown();
super.onDestroy();
}
}
Jetpack Compose
package ...
...
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.LocalContext
import com.google.android.libraries.searchinapps.GetSearchContentViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchContentViewOptions
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchContentViewGenerator
...
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SearchRepeatUI()
}
}
@Composable
fun SearchRepeatUI() {
...
var service by remember {
mutableStateOf<SearchInAppsService?>(
SearchInAppsService.create(LocalContext.current))
}
DisposableEffect(Unit) { onDispose { service?.shutDown() } }
val callback =
object : GetSearchContentViewGeneratorCallback() {
override fun onSuccess(generator: SearchContentViewGenerator) {
...
}
override fun onError(errorMessage: String) {
...
}
}
// Uses the default SearchContentViewOptions.
var options: GetSearchContentViewOptions =
GetSearchContentViewOptions()
.setSearchRepeatContext("sample search repeat")
service?.let { service ->
service.getSearchContentView(options, callback)
}
...
}
}
検索コンテンツの UI ビューを追加する
getSearchContentView の最終的な出力は SearchContentViewGenerator オブジェクトです。このオブジェクトの populateView(Context, int) 関数を使用して UI ビューを生成し、updateView(View, Context) を使用して既存のビューを更新できます(特に Jetpack Compose アプリの場合)。
Jetpack Compose アプリの場合は、AndroidView を使用して 生成された従来のビューを使用する必要があります。
サンプルコード
Java
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchContentViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetSearchContentViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchContentViewGenerator;
import java.util.Arrays;
import java.util.List;
...
public class MainActivity extends AppCompatActivity {
private SearchInAppsService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
service = SearchInAppsService.create(this);
GetSearchContentViewGeneratorCallback searchContentCallback =
new GetSearchContentViewGeneratorCallback() {
@Override
public void onSuccess(SearchContentViewGenerator generator) {
ViewGroup container = findViewById(R.id.[container_id]);
container.removeAllViews();
searchContentBlock = 0;
// If you don't specify "numberOfBlocksToRequest" in
// GetSearchContentViewOptions, by default
// SearchContentViewGenerator.getSearchContentBlockCount() always
// returns 1.
while (searchContentBlock <
generator.getSearchContentBlockCount()) {
container.addView(
generator.populateView(MainActivity.this,
searchContentBlock));
searchContentBlock++;
}
}
@Override
public void onError(String errorMessage) {
...
}
};
// Uses the default SearchContentViewOptions.
service.getSearchContentView(
new GetSearchContentViewOptions().setSearchRepeatContext(
"sample search repeat"), searchContentCallback);
...
}
@Override
public void onDestroy() {
service.shutDown();
super.onDestroy();
}
}
Jetpack Compose
package ...
...
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.libraries.searchinapps.GetSearchContentViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchContentViewOptions
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchContentViewGenerator
...
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { SearchRepeatUI() }
}
@Composable
fun SearchRepeatUI() {
...
var service by remember {
mutableStateOf<SearchInAppsService?>(
SearchInAppsService.create(LocalContext.current))
}
var viewGenerator by remember {
mutableStateOf<SearchContentViewGenerator?>(null) }
var searchContentBlockNumber by remember { mutableStateOf(0) }
DisposableEffect(Unit) { onDispose { service?.shutDown() } }
val callback =
object : GetSearchContentViewGeneratorCallback() {
override fun onSuccess(
generator: SearchContentViewGenerator) {
viewGenerator = generator
// If you don't specify "numberOfBlocksToRequest" in
// GetSearchContentViewOptions, by default
// SearchContentViewGenerator.getSearchContentBlockCount()
// always returns 1.
searchContentBlockNumber =
generator.getSearchContentBlockCount()
}
override fun onError(errorMessage: String) {
...
}
}
// Uses the default SearchContentViewOptions.
var options: GetSearchContentViewOptions =
GetSearchContentViewOptions()
.setSearchRepeatContext("sample search repeat")
service?.getSearchContentView(options, callback)
SearchRepeatUIComposable(viewGenerator, searchContentBlockNumber)
...
}
@Composable
fun SearchRepeatUIComposable(viewGenerator: SearchContentViewGenerator?,
searchContentBlockNumber: Int,) {
viewContentGenerator?.let { viewContentGenerator ->
var context = LocalContext.current
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
for (index in 0..searchContentBlockNumber - 1) {
Row {
AndroidView(
factory = { context ->
viewContentGenerator.populateView(context, index) },
update = { view ->
viewContentGenerator.updateView(view, context) },
)
}
}
}
}
}