Rajitha Perera
Rajitha Perera

Reputation: 1621

Ask Suggestion -Callback function for recycle view when edit texts lost focus when moving up or down

I have activity with recycleView which included some edit texts.when selecting any edit text keyboard getting the popup. But when I moving recycle view to up keyboard changing to default one.

I found the reason for it that when edit text gets disappear it removed from the recycle view so it lost the focus. Then keyboard changing to default.

Now I need to close keyboard when there is no focus for any edit text when moving recycleview.But I do not know how to get that kind of call back when there is no forcus for the edit texts.

Upvotes: 1

Views: 224

Answers (1)

Nursultan Almakhanov
Nursultan Almakhanov

Reputation: 301

I want to suggest you one method of recyclerView called onViewDetachedFromWindow. For example when you scroll the recyclerView, item become invisible and at that moment adapter calls:

override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
        super.onViewAttachedToWindow(holder)
        if (holder is MyViewHolder) {
            holder.checkForFocus()
        }
}

After in viewHolder you may check for focus:

inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bind(item: MyViewItem) {
            itemView.editText.hint = item.title
        }

        fun checkForFocus() {
            if(itemView.editText.hasFocus()){
                callback.hideKeyBoard()
            }
        }
    }
}

var callback: IKeyBoard? = null // initialize it in fragment/activity


interface IKeyBoard {
        fun hideKeyBoard()
}

After that you can implement the callback method in fragment/activity to hide keyboard

I hope that I helped you

Upvotes: 1

Related Questions