如何为注入的InfiniteScrollListener实例提供不同的处理功能-Kotlin [英] how to provide different handling function to the injected InfiniteScrollListener instance - in kotlin

查看:73
本文介绍了如何为注入的InfiniteScrollListener实例提供不同的处理功能-Kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与

似乎我找到了解决方法(感谢@David Medenjak解释了此错误),它可以工作,但不确定是否有更好的方法它尤其是为向注入的InfiniteScrollListener实例证明不同的处理功能提供了灵活性。当前解决方案中,处理功能已在PresentorModule中进行了硬编码。为此,我认为这不是真正解决错误的方法,

Seems I find a work around (thanks @David Medenjak for explain the error), it works, but not sure if there is better way to do it, especially to provide the flexibility of proving different handling function to the injected InfiniteScrollListener instance. Current solution the handling function is hard coded in the PresentorModule. For that I don't think it is a really solution for the error of

@javax.inject.Named("func") kotlin.jvm.functions.Function0<kotlin.Unit> cannot be provided without an @Provides-annotated method.

这是当前解决方案:

带有ViewScope的子组件,现在提供LinearScopeManager和InfiniteScrollListener,它们是此范围的单重态

The subcomponent with ViewScope, now it provides LinearLayoutManager and InfiniteScrollListenerwhich are singlets to this scope

@ViewScope
@Subcomponent(modules = arrayOf(PresentorModule::class))
interface PresentorComponent {

    fun inject (fragment: ArticlesFragment)

    fun getPresenter(): Presentor

    fun getLinearLayoutManager(): LinearLayoutManager
    fun getInfiniteScrollListener(): InfiniteScrollListener

}

PresenterModule提供linearLayoutManager(依赖于此模块的当前上下文),以及InfiniteScrollListener(依赖于presentor和linearLayoutManager),并具有处理功能的硬编码实现

The PresentorModule provides linearLayoutManager (has dependency on the current context of this module), and InfiniteScrollListener which depends on the presentor and the linearLayoutManager, with a hard coded implementation of the handling function

@Module
class PresentorModule {

    var mContext: Context
    var mPresentor: Presentor

    constructor(context: Context) {
        mContext = context
        mPresentor = Presentor() 
    }
    @Provides
    @ViewScope
    internal fun context(): Context {// not sure if it has to implement to provide this mContext
        return mContext
    }

    @Provides
    @ViewScope

    fun presentor() : Presentor {
      return mPresentor      
    }

    @Provides
    @ViewScope

    fun linearLayoutManager() : LinearLayoutManager {
        return LinearLayoutManager(mContext)
    }

    @Provides
    @ViewScope
    fun infiniteScrollListener() : InfiniteScrollListener {
        return InfiniteScrollListener(
            {
                presentor().pullDataFromRemoteServer()
            },
            linearLayoutManager())
    }

 // really would like to have the flexibility of letting the consumer provides the 
 // handling function to the InfiniteScrollListener instance,  
 // but don’t know how to do it, so have to hard code the handling function
 // in the provider of InfiniteScrollListener listed above

 //    @Provides
 //    @ViewScope
 //    fun infiniteScrollListener(func: () -> Unit, layoutManager: LinearLayoutManager) :     InfiniteScrollListener {
 //        return InfiniteScrollListener(func, layoutManager)
 //    }
}

InfiniteScrollListener类的定义,该类具有处理功能,并在onScrolled()中调用

The definition of InfiniteScrollListener class, which takes a handling function and it is called in onScrolled()

class InfiniteScrollListener (val func: () -> Unit,
                          val layoutManager: LinearLayoutManager) : RecyclerView.OnScrollListener() {
    init{
    … …
}

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
    super.onScrolled(recyclerView, dx, dy) 

    … …
    func()
    … …
}

使用的presentorComponent:

The use of presentorComponent:

private lateinit var presentorComponent: PresentorComponent
var infiniteScrollListener: InfiniteScrollListener? = null

@Inject
lateinit var presentor: Presentor

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
    ……

   //use dagger to inject viewScope presentor
    if (MyApp.graph != null) {
        presentorComponent = MyApp.graph
            .addChildModle(PresentorModule(getActivity()))

        presentorComponent
            .inject(this)
    }
    return view
}


override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    articlesList!!.apply {
        setHasFixedSize(true)
        clearOnScrollListeners()

        infiniteScrollListener = presentorComponent.getInfiniteScrollListener()

// old non-injection way to instantiate the InfiniteScrollListener with
// a custom handling function passed in at this moment

//            infiniteScrollListener = InfiniteScrollListener(
//                    {
//                        presentor.pullDataFromRemoteServer()                   
//                    },
//                    linearLayout)

        addOnScrollListener(infiniteScrollListener)

我真的很想在注入
时具有提供不同处理功能的灵活性类InfiniteScrollListener,

I really would like to have the flexibility of provide different handling function when inject the class InfiniteScrollListener,

就像旧代码一样:

infiniteScrollListener = InfiniteScrollListener(
    {
        presentor.pullDataFromRemoteServer()           
    },
    linearLayout)

而不是硬编码在

@Module
class PresentorModule {

… …
    @Provides
    @ViewScope
    fun infiniteScrollListener() : InfiniteScrollListener {
        return InfiniteScrollListener(
            {
                presentor()
            },
            linearLayoutManager())
    }


推荐答案

如果我对您的理解正确,那么您希望注入InfiniteScrollListener的类不知道LinearLayoutManager,而是负责编写执行的代码?

If I understand you correctly you want the Class that injects your InfiniteScrollListener not to know the LinearLayoutManager but to be in charge of writing the code that is executed?

或者,您可以注入InfiniteScrollMethod并为该函数指定一个setter方法。

Alternatively you could inject your InfiniteScrollMethod and give it a setter method for the function.

class InfiniteScrollListener (val layoutManager: LinearLayoutManager) : ...

    var func: (() -> Unit)?=null

    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        super.onScrolled(recyclerView, dx, dy) 
        ...
        func()
        ...
     }
}

然后在您的视图中:

infiniteOnScollListener.func = { code() }
addOnScrollListener(infiniteScrollListener)

另外,请注意:在匕首模块中,无需调用@Provides方法直。例如。

Also on a side note: In your dagger Modules there is no need to call the @Provides methods directly. E.g.

    @Provides
    @ViewScope
    fun provideInfiniteScrollListener(presenter:Presenter,linearLayoutManager: LinearLayoutManager) : InfiniteScrollListener {
        return InfiniteScrollListener(
            {
                presenter
            },
            linearLayoutManager)
    }

Dagger将负责提供Provides方法的参数。

Dagger will take care of providing the arguments of the provides method.

此外,该函数还传递给InfiniteScollListener {presentor()} 除了在被调用时创建一个新的presenter之外不会做任何事情(以防万一,您不知道,并且这不只是在寻找解决方案时)

Also the function passed into InfiniteScollListener {presentor()} will not do anything apart from creating a new presenter when it is called (In case you are not aware and it is not just for the meantime of figuring out your solution).

编辑:我想我可能已经了解您想要做什么:您希望监听器在您的演示者或其他对象上调用一个函数,而视图却不知道

I think I might have understood what you want to do: You want the Listener to call a function on your presenter or something else, without the view knowing about the code?

这怎么办?

 @Provides
@ViewScope
fun provideInfiniteScrollListener(@Named("scrollFunc1") func:()->Unit,presenter:Presenter,linearLayoutManager: LinearLayoutManager) : InfiniteScrollListener {
    return InfiniteScrollListener(func,linearLayoutManager)
}

@Provides
@Named("scrollFunc1")
fun scrollFunc(presenter:Presenter):()->Unit{
   return { presenter.onScrolled() }
}

我想我对此最困惑您的意思是为InfiniteScrollListener提供方法的不同实现。谁是函子的提供者?

I think what I am mostly confused about it what you mean with "provide different implementations of the method to the InfiniteScrollListener". Who is in fact the provider of the func?

这篇关于如何为注入的InfiniteScrollListener实例提供不同的处理功能-Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆