Epoxy: How do I use EpoxyTouchHelper andCallbacks with kotlin and databinding models?

Created on 26 Jan 2019  路  3Comments  路  Source: airbnb/epoxy

Hi I am kind of lost. I try to use swiping and the last function I get working is withTargets. The andCallbacks function refuses to compile in kotlin. I am not sure what I am doing wrong. This is the Code I try to use:

EpoxyTouchHelper.initSwiping(recyclerView)
                .leftAndRight() // Which directions to allow
                .withTargets(IncomingTextMessageLayoutElementBindingModel_::class.java, OutgoingTextMessageLayoutElementBindingModel_::class.java)
                .andCallbacks(object : EpoxyTouchHelper.SwipeCallbacks<IncomingTextMessageLayoutElementBindingModel_>() {
                    override fun onSwipeCompleted(model: IncomingTextMessageLayoutElementBindingModel_?, itemView: View?, position: Int, direction: Int) {
                        //Todo
                    }
                })

Any hints what is wrong with this? It tells me that this is the wrong expected type:

Type mismatch: inferred type is but EpoxyTouchHelper.SwipeCallbacks!>! was expected

Most helpful comment

You are using two different target types, so the callback type needs to be generic. The callback can't specify both target types

EpoxyTouchHelper.initSwiping(recyclerView)
            .leftAndRight() // Which directions to allow
            .withTargets(DataBindingItemBindingModel_::class.java, ItemEpoxyHolder_::class.java)
            .andCallbacks(object: EpoxyTouchHelper.SwipeCallbacks<EpoxyModel<*>?>() {
                override fun onSwipeCompleted(
                    model: EpoxyModel<*>?,
                    itemView: View?,
                    position: Int,
                    direction: Int
                ) {

                }
            })

if you use a single target than the callback type can be specific

        EpoxyTouchHelper.initSwiping(recyclerView)
            .leftAndRight() // Which directions to allow
            .withTarget(DataBindingItemBindingModel_::class.java)
            .andCallbacks(object: EpoxyTouchHelper.SwipeCallbacks<DataBindingItemBindingModel_?>() {
                override fun onSwipeCompleted(
                    model: DataBindingItemBindingModel_?,
                    itemView: View?,
                    position: Int,
                    direction: Int
                ) {

                }
            })

All 3 comments

You are using two different target types, so the callback type needs to be generic. The callback can't specify both target types

EpoxyTouchHelper.initSwiping(recyclerView)
            .leftAndRight() // Which directions to allow
            .withTargets(DataBindingItemBindingModel_::class.java, ItemEpoxyHolder_::class.java)
            .andCallbacks(object: EpoxyTouchHelper.SwipeCallbacks<EpoxyModel<*>?>() {
                override fun onSwipeCompleted(
                    model: EpoxyModel<*>?,
                    itemView: View?,
                    position: Int,
                    direction: Int
                ) {

                }
            })

if you use a single target than the callback type can be specific

        EpoxyTouchHelper.initSwiping(recyclerView)
            .leftAndRight() // Which directions to allow
            .withTarget(DataBindingItemBindingModel_::class.java)
            .andCallbacks(object: EpoxyTouchHelper.SwipeCallbacks<DataBindingItemBindingModel_?>() {
                override fun onSwipeCompleted(
                    model: DataBindingItemBindingModel_?,
                    itemView: View?,
                    position: Int,
                    direction: Int
                ) {

                }
            })

Ah thank you! that makes a lot more sense

@elihart Didn't want to open a new issue as it somehow is related to this:

How can I prevent a swipe from fully removing the item from the list? Basicly that it jumps back in place after the swipe.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

qbait picture qbait  路  3Comments

markus2610 picture markus2610  路  4Comments

jamesarich picture jamesarich  路  5Comments

atonamy picture atonamy  路  4Comments

ailuoyi picture ailuoyi  路  5Comments