1

During the recycling and reuse process of RecyclerView (items/views).​​Why doesn't the copy/paste context menu appear when I long-press on text inside a RecyclerView item? If I use holder.setIsRecyclable(false);, the menu will pop up, so I guess this problem is related to recycling, but I don't know how to fix it.

fun setTextCustomStyle(textView: TextView) {
        val context = textView.context
        
        textView.highlightColor = context.getColor(R.color.mj_color_black_08_transparent)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            textView.setTextSelectHandleLeft(R.drawable.text_select_handle_left_mtrl)
            textView.setTextSelectHandleRight(R.drawable.text_select_handle_right_mtrl)
            textView.setTextSelectHandle(R.drawable.text_select_handle_middle_mtrl)
        }
        
        textView.movementMethod = object : LinkMovementMethod() {
            override fun onTouchEvent(widget: TextView, buffer: Spannable, event: MotionEvent): Boolean {
                if (widget.isTextSelectable) {
                    super.onTouchEvent(widget, buffer, event)
                }
                return false
            }
        }
        
        textView.customSelectionActionModeCallback = object : ActionMode.Callback {
            override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
                Selection.selectAll(textView.text as? Spannable)
                return true
            }

            override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
                return false
            }

            override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
                if (item.itemId == android.R.id.copy) {
                    val plainText = (textView.text ?: "").subSequence(textView.selectionStart, textView.selectionEnd).toString()
                    val clipboardManager = context.getSystemService(ClipboardManager::class.java) as ClipboardManager
                    clipboardManager.apply {
                        val clip = ClipData.newPlainText("plainText", plainText)
                        setPrimaryClip(clip)
                    }
                    ToastUtil.show(R.string.already_copy)
                    mode.finish()
                    return true
                }
                return false
            }

            override fun onDestroyActionMode(mode: ActionMode?) {
                if (textView is EditText) {
                    textView.requestFocus()
                }
            }
        }
        
         textView.setTextIsSelectable(true)
         if (textView is EditText) {
             textView.setOnLongClickListener {
                 if (textView.text.isNullOrEmpty() && textView.isFocused) {
                     textView.setSelection(0)
                 } else {
                     textView.selectAll()
                 }
                 false
             }
         } else {
             textView.setOnLongClickListener {
                 false
             }
         }
     }

this is how i use context menu

1
  • hello everyone, i come here to notice a good news that i fix the bug, i will post my fix later Commented Aug 19 at 12:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.