Show all items in AutocompleteTextView without writing text

前端 未结 12 826
萌比男神i
萌比男神i 2020-12-29 01:05

I have a AutocompleteTextView and it works fine. When I write a word it shows the relevant result but I want to show all items without writing any word in AutocompleteTextVi

12条回答
  •  感动是毒
    2020-12-29 01:50

    Nothing Custom Required.
    

    I tried All solutions but in some case that does not work. For Example one solution work for first time but when you remove text it will not appear. So I dug more and found following solution.

    Suggestions are welcome.

    XML:

    
    
                        
    
    
                    
    

    Kotlin:

    val adapter = ArrayAdapter(context, android.R.layout.select_dialog_item, listItems)
    autoComplete.setAdapter(adapter)
    //threshold specifies the minimum number of characters the user has to type in 
    //the
    //edit box before the drop down list is shown
    autoComplete.threshold = 0
    
    //we have to add check for 0 number of character in edit text. When that 
    //happens, we will show pop up manually
    autoComplete.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {}
    
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
    
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            //first check if length of input is 0
            if(s?.length ?: 0 == 0){
                //if you don't use handler with post delay, the API will hide pop 
                //up, even if you show it. There could be better ways to this, but 
                //I have implemented this and after 100 millis it gives an animated 
                //look
                Handler().postDelayed({
                    //manually show drop down
                    autoComplete.showDropDown()
                }, 100) // with 100 millis of delay
            }
        }
    })
    //when user focus out the view, drop down vanishes. When come back it will not 
    //show, so to cover this scenario add following.
    autoComplete.setOnFocusChangeListener { _, hasFocus ->
        //when gain focus manually show drop down
        if(hasFocus)
            autoComplete.showDropDown()
    }
    

提交回复
热议问题