Android 从片段调用startActionMode
我试图在点击一个按钮时从一个片段启动上下文动作模式菜单。我不知道如何从片段中的onClick方法调用“startActionMode()” 我的碎片Android 从片段调用startActionMode,android,kotlin,android-fragments,contextmenu,Android,Kotlin,Android Fragments,Contextmenu,我试图在点击一个按钮时从一个片段启动上下文动作模式菜单。我不知道如何从片段中的onClick方法调用“startActionMode()” 我的碎片 class MyFragment : Fragment() { ... private var mActionMode: ActionMode? = null // when this button is clicked it should launch the Contextual Action Ba
class MyFragment : Fragment() {
...
private var mActionMode: ActionMode? = null
// when this button is clicked it should launch the Contextual Action Bar (CAB)
fun myCAB() {
// Check to see CAB is currenty active.
if(mActionMode != null) return
mActionMode = mActivity.startActionMode(actionModeCallback)
}
// ActionMode.Callback interface, handles actions for contextual CAB
private val actionModeCallback: ActionMode.Callback = object : ActionMode.Callback {
...
}
}
我试过使用
- 反应性
- 从onAttach引用活动(活动:活动)
找到:android.view.ActionMode?您可以强制执行该活动,然后调用startSupportAction模式
mActionMode = (activity as MainActivity?)!!.startSupportActionMode(actionModeCallback)
下面是代码中的内容
class MyFragment : Fragment() {
...
private var mAction: ActionMode? = null
fun myCAB() {
if(mActionMode!= null) return
// as casts the type if is successful or returns null
mAction = (activity as MainActiivty?)!!.startSupportActionMode(actionModeCallback)
}
private val actionModeCallback: ActionMode.Callback = object : ActionModeCallback {
...
}
}
将导入android.view.ActionMode的导入更改为导入androidx.appcompt.view.ActionMode我当前正在导入androidx.appcomp.view.ActionMode。