طلب أداة إنشاء عروض واجهة مستخدم لمحتوى البحث
لقد أتحنا عرض واجهة المستخدم الخاصة بمحتوى البحث في حزمة تطوير البرامج (SDK).
Search Content
هو مصطلح عام يمثّل أنواعًا متعدّدة من ميزات المحتوى، لذا يُرجى الاطّلاع على الأقسام التالية لمعرفة كيفية طلب كل نوع من ميزات المحتوى على التوالي.
تكرار البحث
للحصول على عرض واجهة المستخدم لإعادة البحث، عليك أولاً الحصول على محتوى أداة إنشاء الإعلانات على شبكة البحث من خلال اتّباع الخطوات التالية:
- يمكنك إلغاء طريقتَي
onSuccess
وonError
في فئةGetSearchContentViewGeneratorCallback
. - أنشِئ مثيلاً للفئة
GetSearchContentViewGeneratorCallback
. - أنشئ مثيلًا لفئة
GetSearchContentViewOptions
واضبط سياق تكرار البحث من خلال استدعاء الطريقةsetSearchRepeatContext
. (اختياري) يقبل هذا العنصر أيضًا عنصرSearchContentViewOptions
الذي يقدّم بعض الخيارات لتخصيص مظهر واجهة المستخدم. - استدعاء دالة
getSearchContentView(GetSearchContentViewOptions, GetSearchContentViewGeneratorCallback)
فيSearchInAppsService
- بعد الحصول على أداة إنشاء واجهة المستخدم، يمكنك التفكير في تخزينها في 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)
}
...
}
}
إضافة عرض واجهة مستخدم محتوى البحث
بالنسبة إلى 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) },
)
}
}
}
}
}