熱門搜尋

如同要求搜尋建議,如要取得熱門搜尋 UI 檢視畫面,請先按照下列步驟取得產生器:

  1. 讓目標 Activity 類別實作 GetSearchSuggestionsViewGeneratorCallback 介面,或使用匿名內部類別。
  2. 覆寫 GetSearchSuggestionsViewGeneratorCallback 介面的 onSuccess(SearchSuggestionsViewGenerator)onError(String) 方法。
  3. 使用指定的熱門搜尋次數上限,建構 GetTrendingSearchesViewOptions 類別例項。(選用) 這個物件也會採用 SearchSuggestionsViewOptions 物件,提供一些選項,可自訂搜尋建議 UI 的外觀。
  4. 呼叫 SearchInAppsServicegetTrendingSearchesView(GetTrendingSearchesViewOptions, GetSearchSuggestionsViewGeneratorCallback) 函式。
  5. 取得 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 檢視畫面的方式與搜尋建議功能相同。

下一篇:顯示搜尋結果