搜索内容

请求搜索内容界面视图生成器

我们在 SDK 中添加了对返回搜索内容界面视图的支持。Search Content 是一个泛指词,表示多种类型的内容地图项,请参阅以下部分,了解如何分别请求每种类型的内容地图项。

搜索重复

如需获取搜索重复的界面视图,您应先按以下步骤获取其搜索内容生成器:

  1. 替换 GetSearchContentViewGeneratorCallback 类的 onSuccessonError 方法。
  2. GetSearchContentViewGeneratorCallback 类创建实例。
  3. 构建 GetSearchContentViewOptions 类实例,并通过调用方法 setSearchRepeatContext 设置搜索重复情境。(可选)该对象还接受 SearchContentViewOptions 对象,该对象提供一些用于自定义界面外观的选项。
  4. 调用 SearchInAppsServicegetSearchContentView(GetSearchContentViewOptions, GetSearchContentViewGeneratorCallback) 函数。
  5. 获取界面生成器后,您可以考虑将其存储在 ViewModel 中,以便在需要重新创建 activity 时(例如在应用运行期间配置发生更改时)无需再次请求生成器。

示例代码

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

添加搜索内容界面视图

对于 getSearchContentView,最终输出是 SearchContentViewGenerator 对象。您可以使用此对象的 populateView(Context, int) 函数生成界面视图,并使用 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) },
          )
        }
      }
    }
  }
}

下一步:显示搜索结果