I'm trying to use ListPopupWindow to create and customize popup. In test project it works okey, but in enother project this popup start scrolling. How to disable this scroll?
This is my PopupAdapter
class PopupAdapter(
private val context: Context,
var list: List<MenuItem>
) : BaseAdapter() {
override fun getCount(): Int {
return list.size
}
override fun getItem(position: Int): MenuItem {
return list[position]
}
override fun getItemId(position: Int): Long {
return list[position].itemId.toLong()
}
@SuppressLint("ViewHolder")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val inflater = LayoutInflater.from(context)
val binding = ItemListPopupWindowBinding.inflate(inflater, parent, false)
val item = getItem(position)
binding.menuItem = item
val deleteText = context.resources.getText(R.string.all_delete_label)
if (item.title == deleteText) {
val color = context.resources.getColor(R.color.colorRed, null)
binding.actionName.setTextColor(color)
binding.icon.setColorFilter(color)
}
return binding.root
}
}
function to create popup
fun createPopup(
context: Context,
view: View,
@MenuRes menuRes: Int,
listener: AdapterView.OnItemClickListener
): ListPopupWindow {
val listPopupWindow = ListPopupWindow(context)
val mMenu = MenuBuilder(context)
MenuInflater(context).inflate(menuRes, mMenu)
val list = mMenu.children.toList()
val adapter = PopupAdapter(context, list)
val width = context.resources.getDimensionPixelSize(R.dimen.popup_width)
val height = ListPopupWindow.WRAP_CONTENT
val background = ContextCompat.getDrawable(context, R.drawable.popup_background)
listPopupWindow.apply {
inputMethodMode = ListPopupWindow.INPUT_METHOD_NOT_NEEDED
setAdapter(adapter)
anchorView = view
this.width = width
this.height = height
setBackgroundDrawable(background)
isModal = true
setOnItemClickListener { parent, listItemView, position, id ->
listener.onItemClick(parent, listItemView, position, id)
listPopupWindow.dismiss()
}
}
return listPopupWindow
}
I'm tryed to disable scroll in parend in adapte, in ListPopupWindow and in ListView, which ListPopupWindow use, but it don't work.
EDITED AND ANSWER
ListPopupWindow start scrolling when window hight is less than height of his elements. When I use data binding to set value in popup menu item, then popup can't correctly calculate his height.
Using binding.executePendingBindings() help in my problem
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val inflater = LayoutInflater.from(context)
val binding = ItemListPopupWindowBinding.inflate(inflater, parent, false)
val item = getItem(position)
binding.menuItem = item
val deleteText = context.resources.getText(R.string.all_delete_label)
if (item.title == deleteText) {
val color = context.resources.getColor(R.color.colorRed, null)
binding.actionName.setTextColor(color)
binding.icon.setColorFilter(color)
}
binding.executePendingBindings() //this line changed
return binding.root
}