使用方法:
build.gradle(project)下定义版本号
buildscript {
ext.kotlin_version = '1.4.10'
ext.dagger_version = "2.28.3"//dragger2 版本号
repositories {
google()
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<RecyclerView>(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()
return 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<Seller> = ArrayList<Seller>()
var sum: Int = 0
var distance: Int = 0
var alpha = 55
private fun initData() {
// for (i in 0 until 100) {
// datas.add("我是商家:" + i)
// }
homeFragmentPresenter.getHomeInfo()
// homeRvAdapter.setData(datas)
}
// 所有的数据
val allList: ArrayList<Seller> = ArrayList()
fun onHomeSuccess(nearbySellers: List<Seller>, otherSellers: List<Seller>) {
allList.clear()
allList.addAll(nearbySellers)
allList.addAll(otherSellers)
homeRvAdapter.setData(allList)
//有数据可以滚动才可以监听滚动事件,设置标题的透明效果
rvHome.setOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (recyclerView != null) {
super.onScrollStateChanged(recyclerView, newState)
}
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (recyclerView != null) {
super.onScrolled(recyclerView, dx, dy)
}
sum += dy
if (sum > distance) {
alpha = 255
} else {
alpha = sum * 200 / distance
alpha += 55
}
Log.e("home", "alpha:$alpha")
ll_title_container.setBackgroundColor(Color.argb(alpha, 0x31, 0x90, 0xe8))
}
})
}
fun onHomeFailed() {
toast("onHomeFailed")
}
}