인기 급상승 검색어

검색어 추천 요청과 마찬가지로 인기 급상승 검색어 UI 뷰를 가져오려면 먼저 다음 단계에 따라 생성기를 가져와야 합니다.

  1. 타겟 Activity 클래스가 GetSearchSuggestionsViewGeneratorCallback 인터페이스를 구현하거나 익명의 내부 클래스를 사용하도록 합니다.
  2. GetSearchSuggestionsViewGeneratorCallback 인터페이스의 onSuccess(SearchSuggestionsViewGenerator)onError(String) 메서드를 재정의합니다.
  3. 지정된 최대 인기 급상승 검색어 수로 GetTrendingSearchesViewOptions 클래스 인스턴스를 만듭니다. (선택사항) 이 객체는 검색 추천 UI의 모양을 맞춤설정하는 몇 가지 옵션을 제공하는 SearchSuggestionsViewOptions 객체도 사용합니다.
  4. SearchInAppsServicegetTrendingSearchesView(GetTrendingSearchesViewOptions, GetSearchSuggestionsViewGeneratorCallback) 함수를 호출합니다.
  5. UI 생성기를 가져온 후에는 ViewModel에 저장하여 활동을 다시 만들어야 할 때 (예: 앱이 실행되는 동안 구성이 변경된 경우) 생성기를 다시 요청하지 않아도 되도록 할 수 있습니다.

샘플 코드

자바

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에 추가하는 방법은 검색 추천 기능과 동일합니다.

다음: 검색 결과 표시