模糊搜索框 EditText动态过滤Listview并改变颜色(仿微信模糊搜索)

模糊搜索框

APP需要一个该控件,安卓端。

先上个图,看起来不错的效果。

图一为未点击状态,图二为点击之后的状态。图三为输入之后的状态。

 

主要的功能点有 :

1、点击直接悬浮层,当前Activity实现不能跳转。

2、实现模糊搜索,EditText动态输入,实现模糊搜索,过滤ListView的资源。

3、EditText的动态输入,Listview包含该字符的,颜色标识不同(高亮)。

4、Json封装传值。

5、EditText带清除按钮(类似ClearEditText),软键盘的自动显示与隐藏。

 

      

 

查看了一下微信等的模糊搜索,看起来不难,呵呵,但还是花费了点时间去弄出来的。

结合了相关资源,嵌入到工程去了。

提取了一下,记录一下,也希望能帮到有需要的朋友。

直接上代码。

 

首先是 控件(开源项目提取的)

EditSearchView:

 

private boolean mIsSearchOpen;
    private boolean mClearingFocus;
    private View mSearchLayout;
    private View view;
    private ListView mSuggestionsListView;
    private EditText mSearchSrcTextView;
    private ImageButton mEmptyBtn;
    private TextView search_cancel;
    private RelativeLayout mSearchTopBar;
    private CharSequence mOldQueryText;
    private CharSequence mUserQuery;
    private EditSearchView.OnQueryTextListener mOnQueryChangeListener;
    private EditSearchView.SearchViewListener mSearchViewListener;
    private BaseAdapter mAdapter;
    private EditSearchView.SavedState mSavedState;
    private Context mContext;
    private boolean shouldAnimate;
    private final OnClickListener mOnClickListener;

    public EditSearchView(Context context) {
        this(context, (AttributeSet) null);
    }

    public EditSearchView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EditSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs);
        this.mIsSearchOpen = false;
        this.mOnClickListener = new OnClickListener() {
            public void onClick(View v) {
                if (v == mEmptyBtn) {
                    EditSearchView.this.mSearchSrcTextView.setText(null);
                } else if (v == mSearchSrcTextView) {
                    EditSearchView.this.showSuggestions();
                } else if (v == search_cancel) {
                    EditSearchView.this.closeSearch();
                }
            }
        };
        this.mContext = context;
        this.shouldAnimate = true;
        this.initiateView();
    }

    private void initiateView() {
        LayoutInflater.from(this.mContext).inflate(R.layout.search_view_layout, this, true);
        this.mSearchLayout = this.findViewById(R.id.search_layout);
        this.mSearchTopBar = (RelativeLayout) this.mSearchLayout.findViewById(R.id.search_top_bar);
        this.mSuggestionsListView = (ListView) this.mSearchLayout.findViewById(R.id.suggestion_list);
        this.mSearchSrcTextView = (EditText) this.mSearchLayout.findViewById(R.id.searchTextView);
        this.mEmptyBtn = (ImageButton) this.mSearchLayout.findViewById(R.id.action_empty_btn);
        this.search_cancel = (TextView) this.mSearchLayout.findViewById(R.id.search_cancel);
        this.mSearchSrcTextView.setOnClickListener(this.mOnClickListener);
        this.mEmptyBtn.setOnClickListener(this.mOnClickListener);
        this.search_cancel.setOnClickListener(this.mOnClickListener);
        this.initSearchView();
        this.mSuggestionsListView.setVisibility(GONE);
    }

    private void initSearchView() {

    
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值