jcenter()
}
接下来在build.gradle(app)下添加
apply plugin: ‘kotlin-kapt’
添加依赖:
//dagger框架
//dagger2 support begin
implementation “com.google.dagger:dagger-android:$dagger_version”
implementation “com.google.dagger:dagger-android-support:$dagger_version” // if you use the support libraries
//annotationProcessor “com.google.dagger:dagger-android-processor:$dagger_version”//java use
//annotationProcessor “com.google.dagger:dagger-compiler:$dagger_version”//java use
kapt “com.google.dagger:dagger-android-processor:$dagger_version”//kotlin use
kapt “com.google.dagger:dagger-compiler:$dagger_version”//kotlin use
//dagger2 support end
HomeFragmentComponent.kt
package com.example.takeout.dagger2.component
import com.example.takeout.ui.fragment.HomeFragment
import com.example.takeout.dagger2.module.HomeFragmentModule
import dagger.Component
@Component(modules = arrayOf(HomeFragmentModule::class)) interface HomeFragmentComponent {
fun inject(homeFragment: HomeFragment)
}
HomeFragmentModule.kt
package com.example.takeout.dagger2.module
import com.example.takeout.presenter.HomeFragmentPresenter
import com.example.takeout.ui.fragment.HomeFragment
import dagger.Module
import dagger.Provides
@Module class HomeFragmentModule(val homeFragment: HomeFragment){
@Provides fun provideHomeFragmentPresenter(): HomeFragmentPresenter {
return HomeFragmentPresenter(homeFragment)
}
}
HomeFragment.kt中使用注解,不再使用homeFragmentPresenster = HomeFragmentPresenster(this)这种耦合的形式
package com.example.takeout.ui.fragment
import android.app.Fragment
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.takeout.R
import com.example.takeout.dagger2.component.DaggerHomeFragmentComponent
import com.example.takeout.dagger2.module.HomeFragmentModule
import com.example.takeout.presenter.HomeFragmentPresenter
import com.example.takeout.ui.adapter.HomeRvAdapter
import com.heima.takeout.model.beans.Seller
import kotlinx.android.synthetic.main.fragment_home.*
import org.jetbrains.anko.find
import org.jetbrains.anko.toast
import javax.inject.Inject
class HomeFragment : Fragment() {
lateinit var homeRvAdapter: HomeRvAdapter//延迟初始化
lateinit var rvHome: RecyclerView
//dragger2注解使用prenster
@Inject
lateinit var homeFragmentPresenter:HomeFragmentPresenter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = View.inflate(activity, R.layout.fragment_home, null)
rvHome = view.find(R.id.rv_home)
//上下布局
rvHome.layoutManager = LinearLayoutManager(activity)
homeRvAdapter = HomeRvAdapter(activity)
rvHome.adapter = homeRvAdapter
// homeFragmentPresenster = HomeFragmentPresenster(this)
//TODO:解耦View层和P层,通过dagger2(基于注解的依赖注入)生成HomeFragmentPresenter
DaggerHomeFragmentComponent.builder().homeFragmentModule(HomeFragmentModule(this)).build().inject(this)
distance = 120.dp2px()
retu
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
rn view
}
fun Int.dp2px(): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
toFloat(), resources.displayMetrics
).toInt()
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
initData()
}
val datas: ArrayList = ArrayList()
var sum: Int = 0
var distance: Int = 0