Chúng tôi cung cấp danh sách các lệnh gọi lại thành phần hiển thị cho mục đích tuỳ chỉnh và giám sát.
Thông báo về hành động nhấp
Bạn có thể truyền một thực thể gọi lại (SearchSuggestionsViewOptions.ClickNotificationHandler) để phản hồi thao tác nhấp vào thành phần.
Mã mẫu
Java
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 com.google.android.libraries.searchinapps.SearchSuggestionsViewOptions;
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"});
SearchSuggestionsViewOptions.ClickNotificationHandler
clickNotificationHandler =
new SearchSuggestionsViewOptions.ClickNotificationHandler() {
@Override
public void clicked(
SearchSuggestionsViewOptions.ClickProperties clickProperties) {
...
}
};
service.getSearchSuggestionsView(
new GetSearchSuggestionsViewOptions().setTextContext(searchContext)
.setSearchSuggestionsViewOptions(
new SearchSuggestionsViewOptions()
.setClickNotificationHandler(clickNotificationHandler)),this);
...
}
@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.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
import com.google.android.libraries.searchinapps.SearchSuggestionsViewOptions
...
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")
var clickNotificationHandler =
object : SearchSuggestionsViewOptions.ClickNotificationHandler() {
override fun clicked(
clickProperties: SearchSuggestionsViewOptions.ClickProperties) {
...
}
}
var options: GetSearchSuggestionsViewOptions =
GetSearchSuggestionsViewOptions().setTextContext(searchContexts)
.setSearchSuggestionsViewOptions(
SearchSuggestionsViewOptions().setClickNotificationHandler(
clickNotificationHandler))
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) },
)
}
}
}