ขอเครื่องมือสร้างมุมมอง UI ของเนื้อหาใน Search
เราได้เพิ่มการรองรับมุมมอง UI ของเนื้อหาการค้นหาที่แสดงอีกครั้งใน SDK
Search Content เป็นคำทั่วไปที่แสดงถึงเนื้อหาหลายประเภท
ฟีเจอร์ โปรดดูส่วนต่อไปนี้เพื่อดูวิธีขอเนื้อหา
ฟีเจอร์แต่ละประเภทตามลำดับ
ค้นหาซ้ำ
หากต้องการดู UI สำหรับการค้นหาซ้ำ คุณควรรับเครื่องมือสร้างเนื้อหาการค้นหา ก่อนโดยทำตามขั้นตอนต่อไปนี้
- ลบล้างเมธอด
onSuccessและonErrorของคลาสGetSearchContentViewGeneratorCallback - สร้างอินสแตนซ์สำหรับคลาส
GetSearchContentViewGeneratorCallback - สร้างอินสแตนซ์ของคลาส
GetSearchContentViewOptionsและตั้งค่าบริบทการค้นหาซ้ำโดยเรียกใช้เมธอดsetSearchRepeatContext(ไม่บังคับ) ออบเจ็กต์นี้ยังใช้ออบเจ็กต์SearchContentViewOptionsซึ่ง มีตัวเลือกบางอย่างในการปรับแต่งลักษณะที่ปรากฏของ UI - เรียกใช้ฟังก์ชัน
getSearchContentView(GetSearchContentViewOptions, GetSearchContentViewGeneratorCallback)ของSearchInAppsService - หลังจากได้รับเครื่องมือสร้าง 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 เพื่อใช้ View แบบคลาสสิกที่สร้างขึ้น
โค้ดตัวอย่าง
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) },
)
}
}
}
}
}