AutoCompleteTextView force to show all items

前端 未结 12 1796
隐瞒了意图╮
隐瞒了意图╮ 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);
            }
        }
    }
    

提交回复
热议问题