การค้นหาที่มาแรง

หากต้องการดูมุมมอง UI ของการค้นหาที่มาแรง คุณควรสร้างเครื่องมือสร้างก่อนโดยทำตามขั้นตอนต่อไปนี้ ซึ่งคล้ายกับการส่งคำขอคำแนะนำการค้นหา

  1. ให้คลาส Activity เป้าหมายใช้อินเทอร์เฟซ GetSearchSuggestionsViewGeneratorCallback หรือใช้คลาสภายในแบบไม่ระบุชื่อ
  2. ลบล้างวิธีการ onSuccess(SearchSuggestionsViewGenerator) และ onError(String) ของอินเทอร์เฟซ GetSearchSuggestionsViewGeneratorCallback
  3. สร้างอินสแตนซ์ของคลาส GetTrendingSearchesViewOptions ด้วยจำนวนการค้นหาที่มาแรงสูงสุดที่ระบุ (ไม่บังคับ) ออบเจ็กต์นี้ยังใช้ออบเจ็กต์ SearchSuggestionsViewOptions ซึ่งมีตัวเลือกบางอย่างในการปรับแต่งลักษณะที่ปรากฏของ UI คำแนะนำการค้นหาด้วย
  4. เรียกใช้ฟังก์ชัน getTrendingSearchesView(GetTrendingSearchesViewOptions, GetSearchSuggestionsViewGeneratorCallback) ของ SearchInAppsService
  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 ของแอปจะเหมือนกับฟีเจอร์คำแนะนำการค้นหา

ถัดไป: แสดงผลการค้นหา