FlowLayout+SearchView实现玩Android搜索
程序员文章站
2022-06-01 10:23:37
...
今天咱们实现玩Android的搜索页面,看看这里面都有哪些好玩的知识点:下面上个搜索页面
接下来主要的代码围绕加载流式布局FlowLayout和历史数据
请求搜索热词接口:
https://www.wanandroid.com//hotkey/json
方法:GET 参数:无
1、对于流式布局可以依赖鸿洋封装好的类:
implementation 'com.hyman:flowlayout-lib:1.1.2'
布局文件中引用部分:
<com.zhy.view.flowlayout.TagFlowLayout
android:id="@+id/hot_search_flow_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:max_select="0" />
2、接下来定义baseurl 和对应的接口
public static String baseurl ="https://www.wanandroid.com/";
/**
* 搜索热词
*/
@GET("hotkey/json")
Call<HotKeyBean> hotkey();
3、网络请求采用Retrofit,严格按照1234步骤,异步请求获取到热门搜索词汇列表
//1初始化Retorfit对象
retrofit = new Retrofit.Builder()
//设置服务器主机地址,非常注意:BaseUrl必须以/结尾,否则报错
.baseUrl(baseurl)
//设置Gson为json的转换器
.addConverterFactory(GsonConverterFactory.create())
.build();
//2.创建业务接口类实例对象,create方法内部实际上是用动态代理的方式帮助我们创建了实例对象
anInterface = retrofit.create(RetrofitInterface.class);
//3.得到请求的封装对象,包含注解的信息,就是url和参数信息
Call<RetrofitInterface.HotKeyBean> call = anInterface.hotkey();
//4.执行异步请求对象
call.enqueue(new Callback<RetrofitInterface.HotKeyBean>() {
@Override
public void onResponse(Call<RetrofitInterface.HotKeyBean> call, Response<RetrofitInterface.HotKeyBean> response) {
final List<RetrofitInterface.HotKeyBean.DataBean> data = response.body().getData();
hotbean = new ArrayList<RetrofitInterface.HotKeyBean.DataBean>();
hotbean.addAll(data);
tagFlowLayout.setAdapter(new TagAdapter<RetrofitInterface.HotKeyBean.DataBean>(hotbean) {
@Override
public View getView(FlowLayout parent, int position, RetrofitInterface.HotKeyBean.DataBean hotKeyBean) {
View view = View.inflate(getApplication(), R.layout.flow_layout_tv, null);
TextView title = view.findViewById(R.id.common_title_tv);
int padding = dp2px(10);
title.setPadding(padding,padding,padding,padding);
title.setText(data.get(position).getName());
title.setTextColor(createRandomColor());
return view;
}
});
}
@Override
public void onFailure(Call<RetrofitInterface.HotKeyBean> call, Throwable t) {
Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_LONG).show();
}
});
4、针对右上角搜索按钮,点击热门搜索图标,点击搜索历史条目,历史清空等操作逻辑
//点击搜索图标
iv_search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
input = et_input.getText().toString();
if(!TextUtils.isEmpty(input)){
getSearchList(input);
}
}
});
//点击热门搜索标签
tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
@Override
public boolean onTagClick(View view, int position, FlowLayout parent) {
getSearchList(hotbean.get(position).getName());
return true;
}
});
//清空历史数据
all_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
historylist.clear();
searchHistoryAdapter.notifyDataSetChanged();
}
});
//点击搜索历史条目
historyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
getSearchList(historylist.get(position));
}
});
public void getSearchList(final String key){
//搜索成功后搜索数据添加到集合中
if(!historylist.contains(key)){
historylist.add(key);
}
searchHistoryAdapter = new SearchHistoryAdapter(MainActivity.this, R.layout.item_search_history, historylist);
historyListView.setAdapter(searchHistoryAdapter);
}
以上就是搜索页面展示的主要代码,具体源码移步下载路径:源码