Search Network में दिखने वाले कॉन्टेंट के यूज़र इंटरफ़ेस (यूआई) व्यू जनरेटर का अनुरोध करना
हमने SDK में, खोज के नतीजों में दिखने वाले कॉन्टेंट के यूज़र इंटरफ़ेस (यूआई) व्यू को फिर से दिखाने की सुविधा जोड़ी है.
Search Content एक सामान्य शब्द है, जो कॉन्टेंट की कई तरह की सुविधाओं को दिखाता है. हर तरह की कॉन्टेंट सुविधा के लिए अनुरोध करने का तरीका जानने के लिए, नीचे दिए गए सेक्शन देखें.
खोज दोहराना
बार-बार खोज करने के लिए, यूज़र इंटरफ़ेस (यूआई) व्यू पाने के लिए, आपको सबसे पहले इन तरीकों से खोज कॉन्टेंट जनरेटर चाहिए होगा:
GetSearchContentViewGeneratorCallbackक्लास केonSuccessऔरonErrorतरीकों को ओवरराइड करें.GetSearchContentViewGeneratorCallbackक्लास के लिए कोई इंस्टेंस बनाएं.GetSearchContentViewOptionsक्लास इंस्टेंस बनाएं औरsetSearchRepeatContextमेथड को कॉल करके, खोज के लिए दोहराए जाने वाले कॉन्टेक्स्ट को सेट करें. (ज़रूरी नहीं) यह ऑब्जेक्ट,SearchContentViewOptionsऑब्जेक्ट भी लेता है. इससे यूज़र इंटरफ़ेस (यूआई) के दिखने का तरीका पसंद के मुताबिक बनाया जा सकता है.SearchInAppsServiceकेgetSearchContentView(GetSearchContentViewOptions, GetSearchContentViewGeneratorCallback)फ़ंक्शन को कॉल करें.- यूज़र इंटरफ़ेस जनरेटर मिलने के बाद, उसे 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) },
)
}
}
}
}
}