从片段Dagger 2 Kotlin android替换片段

时间:2019-03-25 06:27:01

标签: android android-fragments dagger-2 dagger

我遵循了一些教程,并获得了一些有关匕首2及其实现的知识。我按照教程学习并开始练习代码。我的查询是我需要从一个片段更改片段。我知道实际的方法,但是我需要了解这种情况下匕首2中的标准方法。 我有两个片段HomeFragment,PracticeFragment 我需要从HomeFragment调用Practice片段 我将在下面粘贴我的代码

MainActivity

class MainActivity : AppCompatActivity(), MainContract.View {


    @Inject lateinit var presenter: MainContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        injectDependency()
        presenter.attach(this)
    }

    private fun injectDependency() {
     var activityComponent = DaggerActivityComponent.builder()
         .activityModule(ActivityModule(this))
         .build()
        activityComponent.inject(this)
    }

    override fun showHomeFragment() {
            supportFragmentManager.beginTransaction()
                .addToBackStack(null)
                .replace(R.id.frame,HomeFragment().newInstance(),HomeFragment.TAG)
                .commit()
    }
    override fun showPracticeFragment() {

        supportFragmentManager.beginTransaction()
            .addToBackStack(null)
            .replace(R.id.frame,PracticeFragment().newInstance(),PracticeFragment.TAG)
            .commit()

    }

}

MainPresenter

class MainPresenter : MainContract.Presenter {


    private lateinit var view: MainContract.View
    private val subscriptions = CompositeDisposable()

    override fun onDrawerOptionsClick() {

    }

    override fun subscribe() {

    }

    override fun unsubscrbe() {
        subscriptions.clear()
    }

    override fun attach(view: MainContract.View) {
        this.view = view
        view.showHomeFragment()
    }

}

HomeFragment

class HomeFragment : Fragment(),HomeContract.view  {


    @Inject  lateinit var presenter : HomeContract.Presenter

    private lateinit var rootView: View

    fun newInstance(): HomeFragment{
        return HomeFragment()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        injectDependency()
    }

    private fun injectDependency() {
        val fragmentComponent = DaggerFragmentComponent.builder()
            .fragmentmodule(Fragmentmodule()).build()
        fragmentComponent.inject(this)

    }

    override fun onCreateView (inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView =inflater.inflate(R.layout.fragment_home,container,false)

        return rootView
    }

    override fun onViewCreated (view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        presenter.attach(this)
         presenter.subscribe()
   }

    override fun showProgress(boolean: Boolean) {
    }

    override fun loadMessageSuccess(message: String) {

    }
    companion object {
        val TAG: String = "HomeFragment"
    }
}

HomePresenter

class HomePresenter : HomeContract.Presenter {
    lateinit var view: HomeContract.view
    override fun loadMessage() {

    }

    override fun subscribe() {

    }

    override fun unsubscrbe() {

    }

    override fun attach(view: HomeContract.view) {
        this.view = view
    }
}

我也是MVP方法的新手。请检查上面的代码。预先感谢

0 个答案:

没有答案