我想在单击按钮时从片段启动连续动作模式菜单。我不确定如何从片段中的onClick方法调用“ startActionMode()”。
MyFragment
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 {
...
}
}
我尝试使用
两个选项均返回类型未命中匹配项。 必填:androidx.appcompt.view.ActtionMode? 找到:android.view.ActionMode? p>
答案 0 :(得分:0)
您可以投射Activity,然后调用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 {
...
}
}