封装搜索栏,搜索栏对应不同的页面,需要不同的搜索算法,使用策略设计模式
策略接口:
public interface SearchStrategy<T> {
/**
* 筛选出目标在数据组中结果
* @param dataList
* @param target
* @return 符合条件的结果
*/
@NonNull
List<T> getResult(List<T> dataList, String target);
}
一个只有String格式的页面搜索
public class DetailsSearch implements SearchStrategy<String> {
@NonNull
@Override
public List<String> getResult(List<String> dataList, String target) {
List<String> result = new LinkedList<>();
for (String per : dataList){
if (!"".equals(target) && per.contains(target)){
result.add(per);
}
}
return result;
}
}
一个有BeanInfo格式的页面搜索
public class BeansSearch implements SearchStrategy<BeanInfo> {
@NonNull
@Override
public List<BeanInfo> getResult(List<BeanInfo> dataList, String target) {
List<BeanInfo> infos = new LinkedList<>();
try {
SearchTool searchTool = new SearchTool(dataList,"beanName");
List<Object> obs = searchTool.searchTasks(target);
for (int i = 0 ; i < obs.size() ; i++){
infos.add((BeanInfo)obs.get(i));
}
} catch (Exception e) {
e.printStackTrace();
}
return infos;
}
}
在搜索页面使用
public class SearchTopPop<H> extends BasePopup<SearchTopPop<H>> {
@NonNull
private List<H> originData;
private RecyclerView searchRecycleView;
private BaseQuickAdapter adapter;
private Context mContext;
private SearchStrategy<H> searchStrategy;
private List<H> resultData;
private ChooseItemCallback callback;
// public static<T> SearchTopPop create(Context context,@NonNull List<T> originData,@NonNull BaseQuickAdapter adapter
// ,@NonNull SearchStrategy<T> searchStrategy){
// return new SearchTopPop<>(context,originData,adapter,searchStrategy);
// }
public SearchTopPop(Context context, @NonNull List<H> originData, @NonNull BaseQuickAdapter adapter
, @NonNull SearchStrategy<H> searchStrategy){
setContext(context);
this.originData = originData;
this.adapter = adapter;
this.mContext = context;
this.searchStrategy = searchStrategy;
resultData = new ArrayList<>();
}
@Override
protected void initAttributes() {
setContentView(R.layout.search_top_layout, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
setOutsideTouchable(false)
.setFocusable(true)
.setBackgroundDimEnable(true)
.setDimValue(0.6f);
}
@Override
protected void initViews(View view) {
adapter.clearData(resultData);
searchRecycleView = view.findViewById(R.id.search_recycle_view);
searchRecycleView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
searchRecycleView.setAdapter(adapter);
EditText et = view.findViewById(R.id.et_search);
et.addTextChangedListener(new TextWatcherAdapter() {
@Override
public void afterTextChanged(Editable s) {
resultData.clear();
resultData.addAll(searchStrategy.getResult(originData,s.toString()));
adapter.notifyDataSetChanged();
}
});
TextView tv = view.findViewById(R.id.search_cancel);
tv.setOnClickListener(v-> dismiss());
adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
if (callback !=null){
callback.onItemChoose(resultData.get(position));
resultData.clear();
}
}
});
}
public interface ChooseItemCallback {
void onItemChoose(Object item);
}
public void setChooseCallback(ChooseItemCallback callback){
this.callback = callback;
}
@Override
public void onDismiss() {
super.onDismiss();
setChooseCallback(null);
}
}