要求搜尋內容 UI 檢視產生器
我們已在 SDK 中新增支援功能,可在 SDK 中傳回搜尋內容 UI 檢視畫面。Search Content
是代表多種內容功能的一般術語,請參閱下文,瞭解如何分別要求各類型的內容功能。
搜尋重複內容
如要重複執行搜尋的 UI 檢視畫面,建議您先按照下列步驟取得搜尋內容產生器:
- 覆寫
GetSearchContentViewGeneratorCallback
類別的onSuccess
和onError
方法。 - 建立
GetSearchContentViewGeneratorCallback
類別的例項。 - 建構
GetSearchContentViewOptions
類別例項,並呼叫setSearchRepeatContext
方法,設定搜尋重複背景。(選用) 這個物件也會採用SearchContentViewOptions
物件,提供一些選項,可自訂 UI 外觀。 - 呼叫
SearchInAppsService
的getSearchContentView(GetSearchContentViewOptions, GetSearchContentViewGeneratorCallback)
函式。 - 取得 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) },
)
}
}
}
}
}