How to add chips from Material Components library to input field in android?

后端 未结 4 659
长情又很酷
长情又很酷 2020-12-07 23:43

I\'ve seen that in android-P google add new material components library which contains material chips:

Material components for android

Material.io chips us

4条回答
  •  一整个雨季
    2020-12-08 00:12

    All of the previous solutions didn't work for me if you want to achieve a gmail like behaviour with chips on multiple lines. In order to do that I had to avoid using the ChipGroup and instead using a FlexboxLayout.

    your_recipient_layout:

    
    
    
    
        
    
        
    
            
    
        
    
    
    
    
    

    The trick now is adding a new chip to the group but as second last position. Something like this:

    private fun addNewChip(person: String, chipGroup: FlexboxLayout) {
        val chip = Chip(context)
        chip.text = person
        chip.chipIcon = ContextCompat.getDrawable(requireContext(), R.mipmap.ic_launcher_round)
        chip.isCloseIconEnabled = true
        chip.isClickable = true
        chip.isCheckable = false
        chipGroup.addView(chip as View, chipGroup.childCount - 1)
        chip.setOnCloseIconClickListener { chipGroup.removeView(chip as View) }
    }
    

提交回复
热议问题