-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
修改 BasePresenter:继承 DefaultLifecycleObserver,使用 Lifecycle 自动感知生命周期
- Loading branch information
zhumj
committed
Aug 24, 2022
1 parent
4f96e63
commit 1dda7e3
Showing
20 changed files
with
303 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/zhumj/androidkitproject/activity/MainActivity2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.zhumj.androidkitproject.activity | ||
|
||
import android.os.Bundle | ||
import android.view.MenuItem | ||
import com.zhumj.androidkit.base.BaseActivity | ||
import com.zhumj.androidkitproject.adapter.Main2ViewPagerAdapter | ||
import com.zhumj.androidkitproject.databinding.ActivityMain2Binding | ||
import com.zhumj.androidkitproject.mvp.contract.Main2Contract | ||
import com.zhumj.androidkitproject.mvp.presenter.Main2Presenter | ||
|
||
class MainActivity2 : BaseActivity<ActivityMain2Binding, Main2Presenter>(), Main2Contract.View2 { | ||
|
||
override fun getViewBinding(): ActivityMain2Binding { | ||
return ActivityMain2Binding.inflate(layoutInflater) | ||
} | ||
|
||
override fun obtainPresenter(): Main2Presenter { | ||
return Main2Presenter(this) | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
initToolBar(true, isShowTitle = true) | ||
mViewBinding.viewPager2.adapter = Main2ViewPagerAdapter(supportFragmentManager, lifecycle) | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
if (item.itemId == android.R.id.home) { | ||
finish() | ||
return true | ||
} | ||
return super.onOptionsItemSelected(item) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/zhumj/androidkitproject/adapter/Main2ViewPagerAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.zhumj.androidkitproject.adapter | ||
|
||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.viewpager2.adapter.FragmentStateAdapter | ||
import com.zhumj.androidkitproject.fragment.Main2Fragment | ||
|
||
/** | ||
* @Author Created by zhumj | ||
* @Date 2022/8/24 9:50 | ||
* @Description 文件描述 | ||
*/ | ||
class Main2ViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) : | ||
FragmentStateAdapter(fragmentManager, lifecycle) { | ||
|
||
override fun getItemCount(): Int = 3 | ||
|
||
override fun createFragment(position: Int): Fragment { | ||
return Main2Fragment.newInstance( | ||
when (position) { | ||
0 -> { | ||
"Main2 Fragment1" | ||
} | ||
1 -> { | ||
"Main2 Fragment2" | ||
} | ||
else -> { | ||
"Main2 Fragment3" | ||
} | ||
} | ||
) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/zhumj/androidkitproject/fragment/Main2Fragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.zhumj.androidkitproject.fragment | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import com.zhumj.androidkit.base.BaseFragment | ||
import com.zhumj.androidkitproject.databinding.FragmentMain2Binding | ||
import com.zhumj.androidkitproject.mvp.contract.Main2F1Contract | ||
import com.zhumj.androidkitproject.mvp.presenter.Main2F1Presenter | ||
|
||
private const val ARG_PARAM = "param" | ||
|
||
class Main2Fragment : BaseFragment<FragmentMain2Binding, Main2F1Presenter>(), Main2F1Contract.View { | ||
private var param: String = "Main2 Fragment1" | ||
|
||
override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentMain2Binding { | ||
return FragmentMain2Binding.inflate(inflater, container, false) | ||
} | ||
|
||
override fun obtainPresenter(): Main2F1Presenter = Main2F1Presenter(this) | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
arguments?.let { | ||
param = it.getString(ARG_PARAM) ?: "Main2 Fragment1" | ||
} ?: tag.let { | ||
param = it ?: "Main2 Fragment1" | ||
} | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
mPresenter?.tag = param | ||
mViewBinding.tvText.post { | ||
mViewBinding.tvText.text = param | ||
} | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun newInstance(param: String) = | ||
Main2Fragment().apply { | ||
arguments = Bundle().apply { | ||
putString(ARG_PARAM, param) | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/zhumj/androidkitproject/mvp/contract/Main2Contract.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.zhumj.androidkitproject.mvp.contract | ||
|
||
import androidx.lifecycle.LifecycleCoroutineScope | ||
|
||
/** | ||
* 这里是 MVP 定义接口的地方 | ||
*/ | ||
interface Main2Contract { | ||
interface Model2 { | ||
suspend fun queryDates(): List<Int> | ||
} | ||
|
||
interface View2 { | ||
|
||
} | ||
|
||
interface Presenter2 { | ||
fun queryDates(lifecycleScope: LifecycleCoroutineScope) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/zhumj/androidkitproject/mvp/contract/Main2F1Contract.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.zhumj.androidkitproject.mvp.contract | ||
|
||
/** | ||
* 这里是 MVP 定义接口的地方 | ||
*/ | ||
interface Main2F1Contract { | ||
interface Model { | ||
|
||
} | ||
|
||
interface View { | ||
|
||
} | ||
|
||
interface Presenter { | ||
|
||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/zhumj/androidkitproject/mvp/model/Main2Model.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.zhumj.androidkitproject.mvp.model | ||
|
||
import com.zhumj.androidkitproject.mvp.contract.Main2Contract | ||
import kotlinx.coroutines.delay | ||
|
||
/** | ||
* 这里在 MVP 中是真正执行数据请求的地方 | ||
*/ | ||
class Main2Model: Main2Contract.Model2 { | ||
|
||
override suspend fun queryDates(): List<Int> { | ||
delay(2000) | ||
return arrayListOf(0, 1, 2, 3, 4) | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/Main2F1Presenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.zhumj.androidkitproject.mvp.presenter | ||
|
||
import android.util.Log | ||
import com.zhumj.androidkit.base.BasePresenter | ||
import com.zhumj.androidkitproject.mvp.contract.Main2F1Contract | ||
import androidx.lifecycle.LifecycleOwner | ||
|
||
/** | ||
* 这里是 MVP 中对逻辑进行处理的地方,通过 View 回调告诉 Activity 如何更新 UI | ||
*/ | ||
class Main2F1Presenter(view: Main2F1Contract.View): BasePresenter<Main2F1Contract.View>(view), Main2F1Contract.Presenter { | ||
|
||
var tag = "Main2 Fragment1" | ||
|
||
override fun onDestroy(owner: LifecycleOwner) { | ||
Log.d("1111111111111111111", "${tag}: onDestroy(owner: LifecycleOwner)") | ||
super.onDestroy(owner) | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/Main2Presenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.zhumj.androidkitproject.mvp.presenter | ||
|
||
import android.util.Log | ||
import com.zhumj.androidkit.base.BasePresenter | ||
import com.zhumj.androidkitproject.mvp.contract.Main2Contract | ||
import com.zhumj.androidkitproject.mvp.model.Main2Model | ||
import androidx.lifecycle.LifecycleCoroutineScope | ||
import androidx.lifecycle.LifecycleOwner | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* 这里是 MVP 中对逻辑进行处理的地方,通过 View 回调告诉 Activity 如何更新 UI | ||
*/ | ||
class Main2Presenter(view: Main2Contract.View2): BasePresenter<Main2Contract.View2>(view), Main2Contract.Presenter2 { | ||
|
||
private val model = Main2Model() | ||
var dates: List<Int> = ArrayList() | ||
|
||
override fun queryDates(lifecycleScope: LifecycleCoroutineScope) { | ||
lifecycleScope.launch { | ||
dates = model.queryDates().also { | ||
|
||
} | ||
} | ||
} | ||
|
||
override fun onDestroy(owner: LifecycleOwner) { | ||
Log.d("1111111111111111111", "Main2Presenter: onDestroy(owner: LifecycleOwner)") | ||
super.onDestroy(owner) | ||
} | ||
|
||
} |
Oops, something went wrong.