android search: customize suggestion layout

前端 未结 3 1754
一整个雨季
一整个雨季 2021-02-04 03:02

I\'ve been following along with the directions here for both SearchView and dialog implementations. Both are visible below. There are many questions on SO that focus on customiz

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-04 03:50

    This is how you can do it:

    Step 1

    Just create a layout with a RecyclerView or an expandable list or list whichever you want to use.

    Step 2

    In you activity (CityActivity) you need to do this:

    1. Create a handler like this:
    private static class SearchHandler extends Handler {
        private WeakReference mTarget;
    
        SearchHandler(CityActivity target) {
            mTarget = new WeakReference<>(target);
        }
    
        public void setTarget(CityActivity target) {
            mTarget.clear();
            mTarget = new WeakReference<>(target);
        }
    
        @Override
        public void handleMessage(final Message msg) {
            if (msg.what == CityActivity.TRIGGER_SEARCH) {
                CityActivity activity = mTarget.get();
                activity.makeRequest(mSearchText.trim());
            }
        }
    }
    
    1. put a TextChangeListener on your searchEditText:
    public void setTextChangeListener() {
        searchView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mSearchText = searchView.getText().toString();
                if (!mSearchText.trim().isEmpty()) {
                    handler.removeMessages(CitySelectionActivity.TRIGGER_SEARCH);
                    handler.sendEmptyMessageDelayed(CityActivity.TRIGGER_SEARCH,
                            CityActivity.SEARCH_TRIGGER_DELAY_IN_MS);
                } else {
                    suggestList.clear();
                    fillAnything();
                }
            }
    
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }
    

    Here suggestList is the data that is given to your list of

提交回复
热议问题