चर्चित खोजें

खोज के सुझावों का अनुरोध करने की तरह ही, रुझान में चल रही खोजों का यूज़र इंटरफ़ेस (यूआई) व्यू पाने के लिए, आपको पहले इसका जनरेटर चाहिए. इसके लिए, यह तरीका अपनाएं:

  1. अपनी टारगेट Activity क्लास को GetSearchSuggestionsViewGeneratorCallback इंटरफ़ेस लागू करने दें या अनाम इंटरनल क्लास का इस्तेमाल करें.
  2. GetSearchSuggestionsViewGeneratorCallback इंटरफ़ेस के onSuccess(SearchSuggestionsViewGenerator) और onError(String) मेथड को बदलें.
  3. रुझान में चल रही खोजों की तय की गई ज़्यादा से ज़्यादा संख्या के साथ, GetTrendingSearchesViewOptions क्लास का इंस्टेंस बनाएं. (ज़रूरी नहीं) यह ऑब्जेक्ट, SearchSuggestionsViewOptions ऑब्जेक्ट भी लेता है. इसमें, खोज से जुड़े सुझावों के यूज़र इंटरफ़ेस (यूआई) के लुक को पसंद के मुताबिक बनाने के कुछ विकल्प होते हैं.
  4. SearchInAppsService के getTrendingSearchesView(GetTrendingSearchesViewOptions, GetSearchSuggestionsViewGeneratorCallback) फ़ंक्शन को कॉल करें.
  5. यूज़र इंटरफ़ेस (यूआई) जनरेटर मिलने के बाद, उसे 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)
    ...
  }
}

आपके ऐप्लिकेशन के यूज़र इंटरफ़ेस (यूआई) में, रुझान में चल रही खोजों के यूज़र इंटरफ़ेस (यूआई) व्यू को जोड़ने का तरीका, खोज के लिए सुझाव की सुविधा जैसा ही है.

अगला लेख: खोज के नतीजे दिखाना