android search: customize suggestion layout

柔情痞子 提交于 2019-12-03 08:49:14

问题


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 customizing the search box, but few that are about customizing the UI of the suggestions. (The ones that do are about color/font, the defaults for which are fine for me.) I would like to be able to widen the suggestions to take up the whole width of the screen. Is there any way to customize the width using either dialog or SearchView implementations. I'd prefer not to use a library unless that is the only option. If this is possible or easier with one of the implementations that is fine.

Here's what the dialog implementation looks like for me:

Here's what the SearchView implementation looks like for me:


回答1:


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<CityActivity> 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




回答2:


Here is the the repository which picks the place from google address search. It has implementation of full screen search list.

Customized Searchview




回答3:


You should create an activity for search results and write a layout xml file where listview or recyclerview fill width using match_parent value. Content must be search results in adapter.



来源:https://stackoverflow.com/questions/34802764/android-search-customize-suggestion-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!