AutoCompleteTextView force to show all items

前端 未结 12 1738
隐瞒了意图╮
隐瞒了意图╮ 2020-12-25 08:10

There is a moment in my app, that I need to force to show all items in the suggestion list, no matter what the user has typed. How can I do that?

I tried to do somet

相关标签:
12条回答
  • 2020-12-25 08:37

    If you want to show the suggestions instantly, then you have to override enoughToFilter() to make it always return true. To ignore what is given as text input, you have to use performFiltering("", 0) with an empty filtering pattern. The AutoCompleteTextView then shows all suggestions.

    This is solution I've combined from other StackOverflow posts:

    public class InstantAutoComplete extends android.support.v7.widget.AppCompatAutoCompleteTextView {
        public InstantAutoComplete(Context context) {
            super(context);
        }
    
        public InstantAutoComplete(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public InstantAutoComplete(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public boolean enoughToFilter() {
            return true;
        }
    
        @Override
        protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    
            if (focused && getAdapter() != null) {
                performFiltering("", 0);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-25 08:37
    1. autoComplete.setText(" ")
    2. filter { it.contains(constraint.trim(), true) } }

    Note: You have to set text for autoComplete with space text and in filter, you should trim constraint text input and it will show all dropdown list.

    0 讨论(0)
  • 2020-12-25 08:40

    It's actually even easier than Sam listed. Whenever you want to show all, just do:

    performFiltering("", 0);
    

    When the filtering finishes it'll automatically display the drop down with all of the items visible.

    0 讨论(0)
  • 2020-12-25 08:43

    As "ArtOfWarfare" suggested, you can just sub-class and override performFiltering():

    public class My_AutoCompleteTextView extends AutoCompleteTextView {
        public My_AutoCompleteTextView(Context context) {
            super(context);
        }
        public My_AutoCompleteTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public My_AutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void performFiltering(CharSequence text, int keyCode) {
            super.performFiltering("", 0);
        }
    }
    

    I'm using it successfully.

    0 讨论(0)
  • 2020-12-25 08:43

    This is what worked for me:

        public class CustomAutoCompleteTextView extends AutoCompleteTextView {
        public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public boolean enoughToFilter() {
            return true;
        }
    
        @Override
        protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
            if (focused) {
                performFiltering(getText(), 0);
            }
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            this.showDropDown();
            return super.onTouchEvent(event);
        }
    }
    
    0 讨论(0)
  • 2020-12-25 08:47

    You don't define the "moment" when you want to display all the results, so I hope this fits. But try something like this:

    AutoCompleteTextView autoComplete;
    String savedText;
    
    public void showAll() {
        savedText = autoComplete.getText().toString();
        autoComplete.setText("");
        autoComplete.showDropDown();
    }
    
    public void restore() {
        autoComplete.setText(savedText);
    }
    
    0 讨论(0)
提交回复
热议问题