Android search list invisible by default? Can it be done?

天涯浪子 提交于 2019-12-12 02:27:23

问题


Hi guys I have a question:

In short, I have a listview, with a searchbar on top of it, I am also able to start my other activities after filtering the results etc; basically everything works fine and sweet.

My question is this: When launching the main activity the listview is visible (obviously enough). Is there a way to make the listview invisible and only after typing in the searchbar to make the results of the listview become visible? Something like an in app search thing; or am I just imagining things?

I hope that I am not too confusing with what I wrote above.

I am looking forward to your replies and I thank you in advance.


回答1:


Go this way: set your listview visibility to gone or invisible from your xml file:

android:visibility="gone"

Now apply textwatcher on searchbar edittext

edittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // TODO Auto-generated method stub
                //put your search logic here and populate listview.
                //after populating listview set its visibility to visible
                listView.setVisibility(View.VISIBLE);
                //and set listview visibility to GONE again when user erase all text from search edittext
                if(s.length() == 0){
                listView.setVisibility(View.GONE);
                } else {
                listView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {

                // TODO Auto-generated method stub
            }
        });


来源:https://stackoverflow.com/questions/33573960/android-search-list-invisible-by-default-can-it-be-done

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