I have a NestedScrollView
contain a RecyclerView
, each adapter item have
and I want to custom TextView select action use kt ext
fun TextView.customMenuCallBack() {
var selectAll = false
customSelectionActionModeCallback = object : ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return true
}
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
item?.let {
return when (it.itemId) {
android.R.id.selectAll -> {
selectAll = true
false
}
android.R.id.copy -> {
if (selectAll) {
//TODO set content
try {
val clipboard: ClipboardManager = context.getSystemService(AppCompatActivity.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText(context.toString(), content)
clipboard.setPrimaryClip(clip)
} catch (e: Exception) {
e.printStackTrace()
}
}
//make custom action work return true
true
}
else -> {
false
}
}
}
return false
}
override fun onDestroyActionMode(mode: ActionMode?) {
}
}
}
I appear some issues:
menu not hide after click menu item
Affects scrolling, when scrolling forward it suddenly back to nearby selected text position
Not workinig in MIUI ROM
I have fixed part of issues
menu not hide after click menu item
Affects scrolling, when scrolling forward it suddenly back to nearby selected text position
fun TextView.customMenuCallBack() {
var selectAll = false
customSelectionActionModeCallback = object : ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return true
}
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
item?.let {
return when (it.itemId) {
android.R.id.selectAll -> {
selectAll = true
false
}
android.R.id.copy -> {
if (selectAll) {
//TODO set content
try {
val clipboard: ClipboardManager = context.getSystemService(AppCompatActivity.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText(context.toString(), content)
clipboard.setPrimaryClip(clip)
} catch (e: Exception) {
e.printStackTrace()
}
}
//fixed first issue
mode?.finish()
//fixed second issue
clearFocus()
//make custom action work return true
true
}
else -> {
false
}
}
}
return false
}
override fun onDestroyActionMode(mode: ActionMode?) {
}
}
}
update: Custom my NestedScrollView2:NestedScrollView and override
override fun requestChildFocus(child: View?, focused: View?) {
//do nothing prevent focused issued
}