请求热搜界面视图生成器
与请求搜索建议类似,如需获取热门搜索界面视图,您应该首先按照以下步骤获取其生成器:
- 让目标
Activity
类实现GetSearchSuggestionsViewGeneratorCallback
接口或使用匿名内部类。 - 替换
GetSearchSuggestionsViewGeneratorCallback
接口的onSuccess(SearchSuggestionsViewGenerator)
和onError(String)
方法。 - 使用指定的热搜索数量上限构建
GetTrendingSearchesViewOptions
类实例。(可选)此对象还接受SearchSuggestionsViewOptions
对象,该对象提供一些用于自定义搜索建议界面外观的选项。 - 调用
SearchInAppsService
的getTrendingSearchesView(GetTrendingSearchesViewOptions, GetSearchSuggestionsViewGeneratorCallback)
函数。 - 获取界面生成器后,您可以考虑将其存储在 ViewModel 中,以便在需要重新创建 activity 时(例如在应用运行期间配置发生更改时)无需再次请求生成器。
示例代码
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)
...
}
}
添加了热搜界面视图
将热搜索界面视图添加到应用界面的方式与搜索建议功能相同。