EditText onSubmit函数在RecyclerView中清除

问题描述 投票:0回答:1

我对回收者视图有疑问。如果我使用以下代码,则它可以完美工作,直到适配器中有很多项目为止,它必须滚动一点。

整个项目位于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仍然有效。

android kotlin android-recyclerview android-edittext recycler-adapter
1个回答
0
投票

好吧,我通过对onSubmit函数使用不同的方法来修复它。我改变了

fun EditText.onSubmit(func: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->

        if (actionId == EditorInfo.IME_ACTION_DONE) {
            func()
        }

        true

    }
}

to

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

© www.soinside.com 2019 - 2024. All rights reserved.