策略模式封装搜索栏

封装搜索栏,搜索栏对应不同的页面,需要不同的搜索算法,使用策略设计模式

策略接口:

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);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值