arimorty / floatingsearchview

A search view that implements a floating search bar also known as persistent search
https://github.com/arimorty/floatingsearchview/blob/master/README.md
Apache License 2.0
3.54k stars 667 forks source link

How to detect if user have stopped typing. #248

Open surajshrestha opened 7 years ago

surajshrestha commented 7 years ago

Like in SearchView there is this function onQueryTextSubmit(String query) can called when user submits the query. How to do similar thing using this search library ?

bilthon commented 7 years ago

This is what I do:

mSearchView.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() {
    @Override
    public void onSearchTextChanged(String oldQuery, String newQuery) {
        if(newQuery.length() >= MIN_QUERY_LENGTH){
            query = newQuery;
            mHandler.removeCallbacksAndMessages(null);
            mHandler.postDelayed(mSendQuery, QUERY_DELAY);
        }
    }
});

Basically I schedule a runnable for QUERY_DELAY ms in the future. If the user keeps typing, the following call to onSearchTextChanged will override this and schedule a new one. All of this until he finally stops and the last scheduled runnable is allowed to run with the most up to date query.

tninelifehacks commented 6 years ago

Do this

var handler = Handler() mSearchView.setOnQueryChangeListener { oldQuery, newQuery -> if (newQuery.length < 3) mSearchView.clearSuggestions() else { handler.removeCallbacksAndMessages(null) handler.postDelayed({ mPresenter.loadSuggestions(newQuery) }, 500) } }