#android #android-recyclerview #android-gridview #android-textinputedittext
Вопрос:
Я реализовал RecyclerView с элементами GridView. Каждый элемент GridView имеет редактируемый текст для ввода количества, который имеет прослушиватель doOnTextChange. Когда я ввожу какое-то число, оно запускает случайное множество редактируемых текстов из разных элементов.
Адаптер
class ItemSelectorRestaurantAdapter(
itemList: ArrayList<SectionHeaderModel>,
private val itemHeaderListener: ItemHeaderListener,
private val onCustomActionListener: (InvoiceItem?) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>(), Filterable {
private val viewSection = 0
private val viewItem = 1
private var itemList: ArrayList<SectionHeaderModel> = arrayListOf()
private var itemListFiltered: ArrayList<SectionHeaderModel> = arrayListOf()
init {
this.itemList = itemList
itemListFiltered = itemList
}
override fun getItemCount(): Int {
return itemListFiltered.size
}
override fun getItemViewType(position: Int): Int {
return if (itemListFiltered[position].header != null) viewSection else viewItem
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val viewHolder: RecyclerView.ViewHolder = if (viewType == viewItem) {
val v: View = LayoutInflater
.from(parent.context)
.inflate(R.layout.view_item_selector_restaurant, parent, false)
GridViewHolder(v)
} else {
val v: View = LayoutInflater
.from(parent.context)
.inflate(R.layout.view_section_header, parent, false)
SectionViewHolder(v)
}
return viewHolder
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val header: String? = itemListFiltered[position].header
val invoiceItem: InvoiceItem? = itemListFiltered[position].invoiceItem
if (holder is GridViewHolder) {
val quantity = invoiceItem?.quantity ?: 0.0
val itemDetails = "Rs ${invoiceItem?.sellPrice}, ${invoiceItem?.itemName}"
holder.tvItemDetails.text = itemDetails
holder.etItemQuantity.setText(Utils.roundString(quantity, 2))
holder.itemView.requestFocus()
setQuantityAddModifyUI(holder, invoiceItem?.quantity ?: 0.0)
holder.etItemQuantity.doOnTextChanged { text, _, _, _ ->
if (holder.etItemQuantity.hasFocus()) {
val oldQty = if (text == "") "0" else text
invoiceItem?.quantity = oldQty.toDoubleX() ?: 0.0
onCustomActionListener(invoiceItem)
}
}
} else {
val view = holder as SectionViewHolder
view.tvHeader.text = if ((header ?: "").isEmpty()) {
itemHeaderListener.sendHeaderPosition("NO CATEGORY", position)
"NO CATEGORY"
} else {
itemHeaderListener.sendHeaderPosition(header!!, position)
header
}
}
}
inner class SectionViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val tvHeader: TextView = v.findViewById(R.id.tvHeader) as TextView
}
inner class GridViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val containerItemSelectorView: MaterialCardView = v.findViewById(R.id.containerItemSelectorView) as MaterialCardView
val containerQuantityAdd: LinearLayout = v.findViewById(R.id.containerQuantityAdd) as LinearLayout
val containerQuantityModify: LinearLayout = v.findViewById(R.id.containerQuantityModify) as LinearLayout
val tvItemDetails: TextView = v.findViewById(R.id.tvItemDetails) as TextView
val etItemQuantity: TextInputEditText = v.findViewById(R.id.etItemQuantity) as TextInputEditText
val btnIncrease: ImageButton = v.findViewById(R.id.btnIncrease) as ImageButton
val btnDecrease: ImageButton = v.findViewById(R.id.btnDecrease) as ImageButton
}
}
Когда этот слушатель (holder.etItemQuantity.doOnTextChanged) запускает триггеры для нескольких элементов в представлении сетки вместо того, на котором пользователь сосредоточился.
Пожалуйста, помогите…