简体   繁体   English

kotlin Dagger2 适配器中的依赖注入

[英]Dependency Injection in Adapter in kotlin Dagger2

Expected Result预期结果

What am I'm trying to do is to inject my AccountType class to ExpandableAdpter and on click of child view what to check user Type?我想要做的是将我的AccountType类注入到ExpandableAdpter并单击子视图来检查用户类型?

How to implement dagger in Adapter?如何在适配器中实现匕首?

Dagger working fine with Fragment and Activity. Dagger 与 Fragment 和 Activity 配合良好。 only getting null in adapter because unable to initialize adapter to dagger仅在适配器中获取空值,因为无法将适配器初始化为 dagger

Sample for interface in dagger匕首中的界面示例

//Di interface 
interface ActivityComponent : BaseComponent {
// adapter
fun inject(expDragSwipeAdapter: ExpandableDraggableSwipeableAdapter)

}

Adapter onCreate of group and childview group 和 childview 的适配器onCreate

@Inject lateinit var accountType: Accounts
private lateinit var activityComponent: ActivityComponent

override fun onCreateGroupViewHolder(parent: ViewGroup, viewType: Int): MyGroupViewHolder {
    activityComponent.inject(this)
    val inflater = LayoutInflater.from(parent.context)
    val v: View
    if (isDragRequire) {
        v = inflater.inflate(R.layout.row_edit_watchlist, parent, false)
    } else {
        v = inflater.inflate(R.layout.row_watchlist, parent, false)
    }
    return MyGroupViewHolder(v, isDragRequire, mContext)
}

override fun onCreateChildViewHolder(parent: ViewGroup, viewType: Int): MyChildViewHolder {
    activityComponent.inject(this)
    val inflater = LayoutInflater.from(parent.context)
    val v = inflater.inflate(R.layout.row_child_watchlist, parent, false)
    return MyChildViewHolder(v, false)
}

I'm facing the error in this line activityComponent.inject(this)我正面临这一行的错误activityComponent.inject(this)

On The onclick checking AccountType and implement business logic On 点击检查 AccountType 并实现业务逻辑

There is no need to request injection from a Dagger 2 Component inside an Adapter for a RecyclerView or a ListView.对于 RecyclerView 或 ListView,不需要从适配器内的 Dagger 2 组件请求注入。

For Fragments and Activities we have no choice other than to explictly request injection from a Component since these objects are instantiated by the Android OS and we don't "control" the constructors.对于片段和活动,我们别无选择,只能从组件中明确请求注入,因为这些对象是由 Android 操作系统实例化的,我们不“控制”构造函数。

For everything else, including Adapters, you should prefer constructor injection and then setting parameters manually.对于其他一切,包括适配器,您应该更喜欢构造函数注入,然后手动设置参数。

Something idiomatic would look something like the following.一些惯用的东西看起来像下面这样。 Inside your Fragment:在您的 Fragment 中:

class MyFragment : Fragment {

    @Inject
    lateinit var accountsAdapter: accountsAdapter

    @Inject
    lateinit var accountsRepository: AccountsRepository

    //load accounts in onStart or wherever you decide to load
    //when loading finished, execute the following method in a callback

    fun onAccountsLoaded(accounts: Accounts) {
        adapter.setAccounts(accounts)
    }
}

For example, your Adapter could do something like:例如,您的适配器可以执行以下操作:

class Adapter @Inject constructor() : BaseAdapter {

    fun setAccounts(accounts: Accounts) {
        this.accounts = accounts
        notifyDataSetChanged()
    } 
}

You can see the official Google Android Architectural examples for using a ListView with Dagger 2. The link is here您可以查看使用带有 Dagger 2 的 ListView 的官方 Google Android 架构示例。链接在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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