急上昇ワードの UI ビュー ジェネレータのリクエスト
検索候補をリクエストする場合と同様に、急上昇ワードの UI ビューを取得するには、まず次の手順でジェネレータを取得する必要があります。
- ターゲット
Activity
クラスにGetSearchSuggestionsViewGeneratorCallback
インターフェースを実装するか、匿名内部クラスを使用します。 GetSearchSuggestionsViewGeneratorCallback
インターフェースのonSuccess(SearchSuggestionsViewGenerator)
メソッドとonError(String)
メソッドをオーバーライドします。- トレンドの検索の最大数を指定して、
GetTrendingSearchesViewOptions
クラスのインスタンスを作成します。(省略可)このオブジェクトは、検索候補 UI の外観をカスタマイズするためのオプションを提供するSearchSuggestionsViewOptions
オブジェクトも受け取ります。 SearchInAppsService
のgetTrendingSearchesView(GetTrendingSearchesViewOptions, GetSearchSuggestionsViewGeneratorCallback)
関数を呼び出します。- UI ジェネレータを取得したら、ViewModel に保存することを検討してください。これにより、アクティビティの再作成が必要になった場合(アプリの実行中に構成が変更された場合など)に、ジェネレータを再度リクエストする必要がなくなります。
サンプルコード
Java
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetTrendingSearchesViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;
...
public class MainActivity extends AppCompatActivity implements GetSearchSuggestionsViewGeneratorCallback {
private SearchInAppsService service;
@Override
public void onSuccess(SearchSuggestionsViewGenerator generator) {
...
}
@Override
public void onError(String errorMessage) {
...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
service = SearchInAppsService.create(this);
// Uses the default SearchSuggestionsViewOptions.
service.getTrendingSearchesView(
new GetTrendingSearchesViewOptions().setMaxNumTrends(3), this);
...
}
@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.GetSearchSuggestionsViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator
...
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SearchSuggestionsUI()
}
}
@Composable
fun SearchSuggestionsUI() {
...
var service by remember {
mutableStateOf<SearchInAppsService?>(
SearchInAppsService.create(LocalContext.current))
}
DisposableEffect(Unit) { onDispose { service?.shutDown() } }
val callback =
object : GetSearchSuggestionsViewGeneratorCallback {
override fun onSuccess(generator: SearchSuggestionsViewGenerator) {
...
}
override fun onError(errorMessage: String) {
...
}
}
// Uses the default SearchSuggestionsViewOptions.
var options: GetTrendingSearchesViewOptions =
GetTrendingSearchesViewOptions()
.setMaxNumTrends(3)
service?.getTrendingSearchesView(options, callback)
...
}
}
急上昇ワードの UI ビューを追加
急上昇ワードの UI ビューをアプリの UI に追加する方法は、検索候補機能と同じです。