ค้นหาเนื้อหา

ขอเครื่องมือสร้างมุมมอง UI ของเนื้อหาการค้นหา

เราได้เพิ่มการรองรับการส่งคืนมุมมอง UI เนื้อหาการค้นหาใน SDK แล้ว Search Content เป็นคำทั่วไปที่ใช้เรียกฟีเจอร์เนื้อหาหลายประเภท โปรดดูวิธีขอฟีเจอร์เนื้อหาแต่ละประเภทตามหัวข้อต่อไปนี้

การค้นหาซ้ำ

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

  1. ลบล้างเมธอด onSuccess และ onError ของคลาส GetSearchContentViewGeneratorCallback
  2. สร้างอินสแตนซ์สำหรับชั้นเรียน GetSearchContentViewGeneratorCallback
  3. สร้างอินสแตนซ์ของคลาส GetSearchContentViewOptions และตั้งค่าบริบทการค้นหาซ้ำโดยเรียกใช้เมธอด setSearchRepeatContext (ไม่บังคับ) ออบเจ็กต์นี้ยังมีออบเจ็กต์ SearchContentViewOptions ที่มีตัวเลือกบางอย่างเพื่อปรับแต่งรูปลักษณ์ของ UI ด้วย
  4. เรียกใช้ฟังก์ชัน getSearchContentView(GetSearchContentViewOptions, GetSearchContentViewGeneratorCallback) ของ SearchInAppsService
  5. หลังจากได้รับเครื่องมือสร้าง UI แล้ว ให้พิจารณาจัดเก็บไว้ใน ViewModel เพื่อที่คุณจะได้ไม่ต้องขอเครื่องมือสร้างอีกครั้งเมื่อต้องสร้างกิจกรรมขึ้นมาใหม่ (เช่น เมื่อการกําหนดค่ามีการเปลี่ยนแปลงขณะที่แอปทํางานอยู่)

โค้ดตัวอย่าง

Java

package ...;

...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchContentViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetSearchContentViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchContentViewGenerator;
...

public class MainActivity extends AppCompatActivity {
  private SearchInAppsService service;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    ...
    service = SearchInAppsService.create(this);
    GetSearchContentViewGeneratorCallback searchContentCallback =
      new GetSearchContentViewGeneratorCallback() {
        @Override
        public void onSuccess(SearchContentViewGenerator generator) {
          ...
        }

        @Override
        public void onError(String errorMessage) {
          ...
        }
      };

    // Uses the default SearchContentViewOptions.
    service.getSearchContentView(
      new GetSearchContentViewOptions().setSearchRepeatContext(
          "sample search repeat"), searchContentCallback);
    ...
  }

  @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.GetSearchContentViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchContentViewOptions
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchContentViewGenerator
...

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      SearchRepeatUI()
    }
  }

  @Composable
  fun SearchRepeatUI() {
    ...
    var service by remember {
      mutableStateOf<SearchInAppsService?>(
        SearchInAppsService.create(LocalContext.current))
    }
    DisposableEffect(Unit) { onDispose { service?.shutDown() } }
    val callback =
            object : GetSearchContentViewGeneratorCallback() {
              override fun onSuccess(generator: SearchContentViewGenerator) {
                ...
              }

              override fun onError(errorMessage: String) {
                ...
              }
            }
    // Uses the default SearchContentViewOptions.
    var options: GetSearchContentViewOptions =
            GetSearchContentViewOptions()
              .setSearchRepeatContext("sample search repeat")
    service?.let { service ->
      service.getSearchContentView(options, callback)
    }
    ...
  }
}

เพิ่มมุมมอง UI ของเนื้อหาการค้นหา

สำหรับ getSearchContentView เอาต์พุตสุดท้ายคือออบเจ็กต์ SearchContentViewGenerator คุณใช้ฟังก์ชัน populateView(Context, int) ของออบเจ็กต์นี้เพื่อสร้างมุมมอง UI และ updateView(View, Context) เพื่ออัปเดตมุมมองที่มีอยู่ได้ (โดยเฉพาะแอป Jetpack Compose)

สําหรับแอป Jetpack Compose คุณควรใช้ AndroidView เพื่อใช้มุมมองแบบคลาสสิกที่สร้างขึ้น

โค้ดตัวอย่าง

Java

package ...;

...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchContentViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetSearchContentViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchContentViewGenerator;
import java.util.Arrays;
import java.util.List;
...

public class MainActivity extends AppCompatActivity {
  private SearchInAppsService service;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    ...
    service = SearchInAppsService.create(this);
    GetSearchContentViewGeneratorCallback searchContentCallback =
      new GetSearchContentViewGeneratorCallback() {
        @Override
        public void onSuccess(SearchContentViewGenerator generator) {
          ViewGroup container = findViewById(R.id.[container_id]);
          container.removeAllViews();
          searchContentBlock = 0;
          // If you don't specify "numberOfBlocksToRequest" in
          // GetSearchContentViewOptions, by default
          // SearchContentViewGenerator.getSearchContentBlockCount() always
          // returns 1.
          while (searchContentBlock <
            generator.getSearchContentBlockCount()) {
              container.addView(
                  generator.populateView(MainActivity.this,
                    searchContentBlock));
              searchContentBlock++;
          }
        }

        @Override
        public void onError(String errorMessage) {
          ...
        }
      };

    // Uses the default SearchContentViewOptions.
    service.getSearchContentView(
      new GetSearchContentViewOptions().setSearchRepeatContext(
        "sample search repeat"), searchContentCallback);
    ...
  }

  @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.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.libraries.searchinapps.GetSearchContentViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchContentViewOptions
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchContentViewGenerator
...

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent { SearchRepeatUI() }
  }

  @Composable
  fun SearchRepeatUI() {
    ...
    var service by remember {
      mutableStateOf<SearchInAppsService?>(
        SearchInAppsService.create(LocalContext.current))
    }
    var viewGenerator by remember {
        mutableStateOf<SearchContentViewGenerator?>(null) }
    var searchContentBlockNumber by remember { mutableStateOf(0) }
    DisposableEffect(Unit) { onDispose { service?.shutDown() } }
    val callback =
            object : GetSearchContentViewGeneratorCallback() {
              override fun onSuccess(
                  generator: SearchContentViewGenerator) {
                viewGenerator = generator
                // If you don't specify "numberOfBlocksToRequest" in
                // GetSearchContentViewOptions, by default
                // SearchContentViewGenerator.getSearchContentBlockCount()
                // always returns 1.
                searchContentBlockNumber =
                  generator.getSearchContentBlockCount()
              }

              override fun onError(errorMessage: String) {
                ...
              }
            }
    // Uses the default SearchContentViewOptions.
    var options: GetSearchContentViewOptions =
            GetSearchContentViewOptions()
              .setSearchRepeatContext("sample search repeat")
    service?.getSearchContentView(options, callback)
    SearchRepeatUIComposable(viewGenerator, searchContentBlockNumber)
    ...
}

  @Composable
  fun SearchRepeatUIComposable(viewGenerator: SearchContentViewGenerator?,
  searchContentBlockNumber: Int,) {
    viewContentGenerator?.let { viewContentGenerator ->
      var context = LocalContext.current
      Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
      for (index in 0..searchContentBlockNumber - 1) {
        Row {
          AndroidView(
            factory = { context ->
              viewContentGenerator.populateView(context, index) },
            update = { view ->
              viewContentGenerator.updateView(view, context) },
          )
        }
      }
    }
  }
}

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