جستجو در محتوا

درخواست تولید کننده نمای رابط کاربری محتوای جستجو کنید

ما پشتیبانی از بازگرداندن نمای رابط کاربری محتوای جستجو را در SDK اضافه کرده‌ایم. Search Content یک اصطلاح کلی است که چندین نوع ویژگی محتوا را نشان می دهد، بخش های زیر را برای نحوه درخواست هر نوع ویژگی محتوا به ترتیب بررسی کنید.

تکرار جستجو

برای دریافت نمای UI برای تکرار جستجو، ابتدا باید مولد محتوای جستجوی آن را با مراحل زیر دریافت کنید:

  1. روش های onSuccess و onError کلاس GetSearchContentViewGeneratorCallback لغو کنید.
  2. یک نمونه برای کلاس GetSearchContentViewGeneratorCallback ایجاد کنید.
  3. نمونه کلاس GetSearchContentViewOptions را بسازید و با فراخوانی متد setSearchRepeatContext زمینه تکرار جستجو را تنظیم کنید. (اختیاری) این شیء همچنین یک شی SearchContentViewOptions می گیرد که گزینه هایی را برای سفارشی کردن ظاهر رابط کاربری ارائه می دهد.
  4. تابع getSearchContentView(GetSearchContentViewOptions, GetSearchContentViewGeneratorCallback) SearchInAppsService را فراخوانی کنید.
  5. بعد از اینکه مولد رابط کاربری را دریافت کردید، می‌توانید آن را در ViewModel ذخیره کنید تا زمانی که فعالیت باید دوباره ایجاد شود (مثل زمانی که پیکربندی در حین اجرای برنامه تغییر کرده است) نیازی به درخواست مجدد ژنراتور نداشته باشید.

کد نمونه

جاوا

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) این شی برای ایجاد نمای رابط کاربری و updateView(View, Context) برای به‌روزرسانی نمای موجود (مخصوصاً برای برنامه‌های Jetpack Compose) استفاده کنید.

برای برنامه‌های Jetpack Compose، باید از AndroidView برای مصرف نمای کلاسیک تولید شده استفاده کنید.

کد نمونه

جاوا

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) },
          )
        }
      }
    }
  }
}

بعدی: نمایش نتایج جستجو