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.
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
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()
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 = ArrayList()
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 = ArrayList()
fun onHomeSuccess(nearbySellers: List, otherSellers: List) {
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