Trình tạo thành phần hiển thị giao diện người dùng cho nội dung tìm kiếm thịnh hành
Tương tự như yêu cầu đề xuất tìm kiếm, để có chế độ xem giao diện người dùng về nội dung tìm kiếm thịnh hành, trước tiên, bạn nên lấy trình tạo của chế độ xem đó theo các bước sau:
- Cho phép lớp
Activity
mục tiêu triển khai giao diệnGetSearchSuggestionsViewGeneratorCallback
hoặc sử dụng lớp bên trong ẩn danh. - Ghi đè các phương thức
onSuccess(SearchSuggestionsViewGenerator)
vàonError(String)
của giao diệnGetSearchSuggestionsViewGeneratorCallback
. - Tạo thực thể lớp
GetTrendingSearchesViewOptions
với số lượng tối đa các cụm từ tìm kiếm thịnh hành được chỉ định. (Không bắt buộc) Đối tượng này cũng lấy một đối tượngSearchSuggestionsViewOptions
cung cấp một số tuỳ chọn để tuỳ chỉnh giao diện người dùng của nội dung đề xuất tìm kiếm. - Gọi hàm
getTrendingSearchesView(GetTrendingSearchesViewOptions, GetSearchSuggestionsViewGeneratorCallback)
củaSearchInAppsService
. - Sau khi tải trình tạo giao diện người dùng, bạn có thể cân nhắc việc lưu trữ trình tạo trong ViewModel để không cần yêu cầu lại trình tạo khi cần tạo lại hoạt động (chẳng hạn như khi cấu hình thay đổi trong khi ứng dụng đang chạy).
Mã mẫu
Java
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetTrendingSearchesViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;
...
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);
// Uses the default SearchSuggestionsViewOptions.
service.getTrendingSearchesView(
new GetTrendingSearchesViewOptions().setMaxNumTrends(3), 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.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) {
...
}
}
// Uses the default SearchSuggestionsViewOptions.
var options: GetTrendingSearchesViewOptions =
GetTrendingSearchesViewOptions()
.setMaxNumTrends(3)
service?.getTrendingSearchesView(options, callback)
...
}
}
Thêm thành phần hiển thị giao diện người dùng cho nội dung tìm kiếm thịnh hành
Cách thêm thành phần hiển thị giao diện người dùng về nội dung tìm kiếm thịnh hành vào giao diện người dùng của ứng dụng cũng giống như tính năng đề xuất tìm kiếm.