简体   繁体   中英

How to add your own function in ActionMode class in Android?

I want to implement an ActionMode which acts as a selection mode . I want to add a function toggle() in my ActionMode which toggles the selection of my list.

my ActionMode code:

class Selection(/* some parameters */): ActionMode.Callback {
    private val selectedPositions = ArrayList<Int>()

    fun toggle(position: Int) {
        if (selectedPositions.contains(position)) {
            selectedPositions.remove(position)
        } else {
            selectedPositions.add(position)
        }
    }

    override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { /* some code */ }
    override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { /* some code */ }
    override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean = false
    override fun onDestroyActionMode(mode: ActionMode?) { /* some code */ }
}

My problem is that I am not able to use the toggle() function from my calling activity/fragment. For example:

private var actionMode: ActionMode? = null
// initialising action mode
if (actionMode == null) {
    actionMode = activity!!.startActionMode(Selection(/* some parameters */))
}
// I am not able to do this
actionMode!!.toggle(position)

Because the function is on your Selection class, not the ActionMode class. Call it on the instnce of the Selection class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM