অনুসন্ধান পরামর্শ অনুরোধ UI ভিউ জেনারেটর
অনুসন্ধান পরামর্শ UI ভিউ পেতে, আপনাকে প্রথমে নিম্নলিখিত পদক্ষেপগুলি দ্বারা এটির জেনারেটর পেতে হবে:
- আপনার টার্গেট
Activity
ক্লাসকেGetSearchSuggestionsViewGeneratorCallback
ইন্টারফেস বাস্তবায়ন করতে দিন বা বেনামী অভ্যন্তরীণ ক্লাস ব্যবহার করতে দিন। -
GetSearchSuggestionsViewGeneratorCallback
ইন্টারফেসেরonSuccess(SearchSuggestionsViewGenerator)
এবংonError(String)
পদ্ধতিগুলিকে ওভাররাইড করুন৷ - অনুসন্ধান প্রসঙ্গের একটি তালিকা সহ
GetSearchSuggestionsViewOptions
ক্লাসের উদাহরণ তৈরি করুন। (ঐচ্ছিক) এই অবজেক্টটি একটিSearchSuggestionsViewOptions
অবজেক্টও নেয় যা অনুসন্ধান পরামর্শ UI এর চেহারা কাস্টমাইজ করার জন্য কিছু বিকল্প প্রদান করে। -
SearchInAppsService
এরgetSearchSuggestionsView(GetSearchSuggestionsViewOptions, GetSearchSuggestionsViewGeneratorCallback)
ফাংশন কল করুন। - আপনি UI জেনারেটর পাওয়ার পরে, আপনি এটিকে একটি ViewModel-এ সংরক্ষণ করার কথা বিবেচনা করতে পারেন যাতে কার্যকলাপটি পুনরায় তৈরি করার প্রয়োজন হলে আপনাকে আবার জেনারেটরের জন্য জিজ্ঞাসা করতে না হয় (যেমন যখন অ্যাপটি চলাকালীন কনফিগারেশন পরিবর্তিত হয়)।
নমুনা কোড
জাভা
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;
import java.util.Arrays;
import java.util.List;
...
public class MainActivity extends AppCompatActivity implements GetSearchSuggestionsViewGeneratorCallback {
private SearchInAppsService service;
@Override
public void onSuccess(SearchSuggestionsViewGenerator generator) {
...
}
@Override
public void onError(String errorMessage) {
...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
service = SearchInAppsService.create(this);
// Right now only the first element of this list will be used.
List<String> searchContext = Arrays.asList(new String[]{"This is a test query, for example."});
// Uses the default SearchSuggestionsViewOptions.
service.getSearchSuggestionsView(
new GetSearchSuggestionsViewOptions().setTextContext(searchContext),this);
...
}
@Override
public void onDestroy() {
service.shutDown();
super.onDestroy();
}
}
জেটপ্যাক রচনা
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.GetSearchSuggestionsViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator
...
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SearchSuggestionsUI()
}
}
@Composable
fun SearchSuggestionsUI() {
...
var service by remember {
mutableStateOf<SearchInAppsService?>(
SearchInAppsService.create(LocalContext.current))
}
DisposableEffect(Unit) { onDispose { service?.shutDown() } }
val callback =
object : GetSearchSuggestionsViewGeneratorCallback {
override fun onSuccess(generator: SearchSuggestionsViewGenerator) {
...
}
override fun onError(errorMessage: String) {
...
}
}
var searchContexts: List<String> = listOf<String>("Query")
// Uses the default SearchSuggestionsViewOptions.
var options: GetSearchSuggestionsViewOptions =
GetSearchSuggestionsViewOptions()
.setTextContext(searchContexts)
service?.getSearchSuggestionsView(options, callback)
...
}
}
অবস্থান-ভিত্তিক অনুসন্ধান পরামর্শের অনুরোধ করুন
getSearchSuggestionsView
এন্ট্রি পয়েন্টটি অবস্থান-ভিত্তিক অনুসন্ধান পরামর্শকেও সমর্থন করে। এই পদক্ষেপগুলি অনুসরণ করে LocationContext
অবজেক্টগুলির একটি তালিকা সহ একটি GetSearchSuggestionsViewOptions
অবজেক্ট তৈরি করুন৷ তারপরে, পূর্ববর্তী বিভাগে দেওয়া পদক্ষেপগুলি অনুসরণ করে getSearchSuggestionsView
ফাংশনে GetSearchSuggestionsViewOptions
পাস করুন।
- (প্রয়োজনীয়)
GeographicalRestrictions
অবজেক্টের সাথেLocationContext
অবজেক্ট তৈরি করুন।GeographicalRestrictions
বিধিনিষেধ বস্তুটি একটিCircularArea
অবজেক্ট নেয় যা এলাকার কেন্দ্রের অক্ষাংশ এবং দ্রাঘিমাংশ এবং এলাকার ব্যাসার্ধ ধারণ করে। - (ঐচ্ছিক)
TimeSegment
অবজেক্ট দিয়েTimeRestrictions
অবজেক্ট তৈরি করুন।TimeSegment
অবজেক্টটি একটি স্ট্রিং নেয় যা ISO 8601 ফরম্যাটে টাইম সেগমেন্টকে প্রতিনিধিত্ব করে। - (ঐচ্ছিক) অনুরোধ করা জিও প্রকারের সাথে
GeoTypeRestrictions
অবজেক্ট তৈরি করুন। প্রদত্ত প্রকারগুলি পরিষেবা দ্বারা প্রত্যাবর্তিত ফলাফলগুলির র্যাঙ্কিং এবং ফিল্টারিং সামঞ্জস্য করতে ব্যবহৃত হয়। সমর্থিত মানগুলি হল: "রেস্তোরাঁ", "ক্যাফে", "বার", "পার্ক", "চিড়িয়াখানা", "জাদুঘর", "আকর্ষণ", "এটিএম", "ব্যাংক", "হেয়ার_স্যালন", "রিয়েল_এস্টেট_এজেন্সি", " সাইকেল_শেয়ারিং_লোকেশন", "কার_ভাড়া_এজেন্সি", "শপিং_সেন্টার", "মুদির_স্টোর" এবং "হোটেল"।
নমুনা কোড
জাভা
package ...;
...
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions;
import com.google.android.libraries.searchinapps.LocationContext;
import com.google.android.libraries.searchinapps.LocationContext.CircularArea;
import com.google.android.libraries.searchinapps.LocationContext.GeoTypeRestrictions;
import com.google.android.libraries.searchinapps.LocationContext.GeographicalRestrictions;
import com.google.android.libraries.searchinapps.LocationContext.LatLng;
import com.google.android.libraries.searchinapps.LocationContext.TimeRestrictions;
import com.google.android.libraries.searchinapps.LocationContext.TimeSegment;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;
...
LocationContext locationContext = new LocationContext(
new GeographicalRestrictions(
new CircularArea(
new LatLng(
40.7414728,
-74.0059622),
1000)))
// Optional. If set, the returned suggestions are categories of which
// there are several businesses in the requested area that are open at
// the given time.
.setTimeRestrictions(
new TimeRestrictions(
new TimeSegment("2024-05-18T19:20:30.45+01:00")))
// Optional. If set, the returned suggestions are subset of the
// requested categories.
.setGeoTypeRestrictions(
new GeoTypeRestrictions("restaurant", "parks"));
// Uses the default SearchSuggestionsViewOptions.
service = SearchInAppsService.create(this);
// Uses the default SearchSuggestionsViewOptions.
service.getSearchSuggestionsView(
new GetSearchSuggestionsViewOptions().setLocationContext(Arrays.asList(locationContext)), this);
জেটপ্যাক রচনা
package ...
...
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions
import com.google.android.libraries.searchinapps.LocationContext
import com.google.android.libraries.searchinapps.LocationContext.CircularArea
import com.google.android.libraries.searchinapps.LocationContext.GeoTypeRestrictions
import com.google.android.libraries.searchinapps.LocationContext.GeographicalRestrictions
import com.google.android.libraries.searchinapps.LocationContext.LatLng
import com.google.android.libraries.searchinapps.LocationContext.TimeRestrictions
import com.google.android.libraries.searchinapps.LocationContext.TimeSegment
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator
...
var locationContext: LocationContext = LocationContext(
GeographicalRestrictions(
CircularArea(
LatLng(40.7414728, -74.0059622),
1000)))
// Optional. If set, there are several businesses in the requested area that
// are open at the given time.
.setTimeRestrictions(
TimeRestrictions(
TimeSegment("2024-05-18T19:20:30.45+01:00")))
// Optional. If set, the returned suggestions are subset of the requested
// categories.
.setGeoTypeRestrictions(
GeoTypeRestrictions("restaurant", "parks"))
// Uses the default SearchSuggestionsViewOptions.
var options: GetSearchSuggestionsViewOptions =
GetSearchSuggestionsViewOptions()
.setLocationContext(listOf<LocationContext>(locationContext))
service.getSearchSuggestionsView(options, callback)
...
অনুসন্ধান পরামর্শ UI ভিউ যোগ করুন
getSearchSuggestionsView
এর জন্য, চূড়ান্ত আউটপুট হল একটি SearchSuggestionsViewGenerator
অবজেক্ট। আপনি এই অবজেক্টের populateView(Context)
ফাংশন ব্যবহার করতে পারেন ইউআই ভিউ তৈরি করতে এবং বিদ্যমান ভিউ updateView(View)
(বিশেষ করে জেটপ্যাক কম্পোজ অ্যাপের জন্য) আপডেট করতে।
জেটপ্যাক কম্পোজ অ্যাপের জন্য, জেনারেট করা ক্লাসিক ভিউ ব্যবহার করতে আপনার AndroidView ব্যবহার করা উচিত।
নমুনা কোড
জাভা
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;
import java.util.Arrays;
import java.util.List;
...
public class MainActivity extends AppCompatActivity implements GetSearchSuggestionsViewGeneratorCallback {
private SearchInAppsService service;
@Override
public void onSuccess(SearchSuggestionsViewGenerator generator) {
ViewGroup container = findViewById(R.id.[container_id]);
container.removeAllViews();
container.addView(generator.populateView(this));
}
@Override
public void onError(String errorMessage) {
...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
service = SearchInAppsService.create(this);
List<String> searchContext = Arrays.asList(new String[]{"Query"});
// Uses the default SearchSuggestionsViewOptions.
service.getSearchSuggestionsView(
new GetSearchSuggestionsViewOptions().setTextContext(searchContext),this);
...
}
@Override
public void onDestroy() {
service.shutDown();
super.onDestroy();
}
}
জেটপ্যাক রচনা
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.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator
...
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { SearchSuggestionsUI() }
}
@Composable
fun SearchSuggestionsUI() {
...
var service by remember {
mutableStateOf<SearchInAppsService?>(
SearchInAppsService.create(LocalContext.current))
}
var viewGenerator = mutableStateOf<SearchSuggestionsViewGenerator?>(null)
DisposableEffect(Unit) { onDispose { service?.shutDown() } }
val callback =
object : GetSearchSuggestionsViewGeneratorCallback {
override fun onSuccess(generator: SearchSuggestionsViewGenerator) {
viewGenerator.value = generator
}
override fun onError(errorMessage: String) {}
}
var searchContexts: List<String> = listOf<String>("Query")
// Uses the default SearchSuggestionsViewOptions.
var options: GetSearchSuggestionsViewOptions =
GetSearchSuggestionsViewOptions().setTextContext(searchContexts)
service?.getSearchSuggestionsView(options, callback)
ChipGroupUI(viewGenerator)
...
}
@Composable
fun ChipGroupUI(viewGenerator: MutableState<SearchSuggestionsViewGenerator?>) {
viewGenerator.value?.let { viewGenerator ->
var context = LocalContext.current
AndroidView(
factory = { context -> viewGenerator.populateView(context) },
update = { view -> viewGenerator.updateView(view, context) },
)
}
}
}