急上昇ワードの 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 ビューを追加する方法は、検索候補機能と同じです。