我对回收者视图有疑问。 如果我使用以下代码,则它可以完美工作,直到适配器中有很多项目为止,它必须滚动一点。
整个项目位于github
class ActionCardAdapter(val round: Round, val activity: RoundActivity)
: RecyclerView.Adapter<ActionCardAdapter.AbstractActionCardViewHolder>() {
abstract class AbstractActionCardViewHolder(view: View) : RecyclerView.ViewHolder(view)
class ComplexActionCardViewHolder(view: View) : AbstractActionCardViewHolder(view) {
val actionNameTextView = view.findViewById<TextView>(R.id.actionNameTextView)
val inputEditText = view.findViewById<EditText>(R.id.actionInputEditText)
}
// other ViewHolders
enum class ViewType {
COMPLEX_ACTION_CARD
// other ViewTypes
}
override fun getItemViewType(position: Int) = when (round.cards[position]) {
is ActionCardComplex -> ViewType.DISPLAY_CARD.ordinal
// other cases
}
override fun onCreateViewHolder(parent: ViewGroup, type: Int): AbstractActionCardViewHolder {
return when (type) {
ViewType.COMPLEX_ACTION_CARD.ordinal -> {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.card_action_complex, parent, false) as View
ComplexActionCardViewHolder(view)
}
// other cases
else -> throw Exception("Card Type not defined.")
}
}
override fun onBindViewHolder(viewHolder: AbstractActionCardViewHolder, position: Int) {
val card = round.cards[position]
when (getItemViewType(position)) {
ViewType.COMPLEX_ACTION_CARD.ordinal -> {
viewHolder as ComplexActionCardViewHolder
card as ActionCardComplex
viewHolder.actionNameTextView.text = card.displayText
fun getInput() {
// some logic
}
viewHolder.itemView.setOnClickListener { getInput() }
viewHolder.inputEditText.onSubmit { getInput() }
viewHolder.inputEditText.requestFocus()
}
}
// other cases
}
override fun getItemCount() = round.cards.size
}
因此,如果我添加这些ActionCardComplex之一,并且喜欢5张绑定不同的卡,
EditText viewHolder.inputEditText
的onSubmit属性似乎已清除。
另一方面,ItemView viewHolder.itemView
的onClickListener仍然有效。
答案 0 :(得分:0)
好的,我通过对onSubmit函数使用不同的方法来修复它。 我改变了
fun EditText.onSubmit(func: () -> Unit) {
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
func()
}
true
}
}
到
fun EditText.onSubmit(func: () -> Unit) = setOnEditorActionListener {
_, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT)
func()
true
}
因为当项目数量太大而无法容纳在屏幕上时,键盘会将其右下角的操作按钮更改为EditorInfo.IME_ACTION_NEXT
而不是EditorInfo.IME_ACTION_DONE
。