增加离线地图下载流程
This commit is contained in:
@@ -7,6 +7,7 @@ import android.widget.Toast
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.lifecycle.Observer
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import com.navinfo.omqs.R
|
||||
import com.navinfo.omqs.databinding.ActivityLoginBinding
|
||||
@@ -30,41 +31,49 @@ class LoginActivity : PermissionsActivity() {
|
||||
initView()
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
//登录校验,初始化成功
|
||||
viewModel.loginStatus.observe(this) {
|
||||
when (it) {
|
||||
LoginStatus.LOGIN_STATUS_NET_LOADING -> {
|
||||
loginDialog("验证用户信息...")
|
||||
}
|
||||
LoginStatus.LOGIN_STATUS_FOLDER_INIT -> {
|
||||
loginDialog("检查本地数据...")
|
||||
}
|
||||
LoginStatus.LOGIN_STATUS_FOLDER_FAILURE -> {
|
||||
Toast.makeText(this, "文件夹初始化失败", Toast.LENGTH_SHORT).show()
|
||||
loginDialog?.dismiss()
|
||||
loginDialog = null
|
||||
}
|
||||
LoginStatus.LOGIN_STATUS_NET_FAILURE -> {
|
||||
Toast.makeText(this, "网络访问失败", Toast.LENGTH_SHORT).show()
|
||||
loginDialog?.dismiss()
|
||||
loginDialog = null
|
||||
}
|
||||
LoginStatus.LOGIN_STATUS_SUCCESS -> {
|
||||
val intent = Intent(this@LoginActivity, MainActivity::class.java)
|
||||
startActivity(intent)
|
||||
/**
|
||||
* 观察登录状态,把Observer提出来是为了防止每次数据变化都会有新的observer创建
|
||||
* 还有为了方便释放(需不需要手动释放?不清楚)不需要释放,当viewmodel观察到activity/fragment 的生命周期时会自动释放
|
||||
* PS:不要在 observer 中修改 LiveData 的值的数据,会影响其他 observer
|
||||
*/
|
||||
private val loginObserve = Observer<LoginStatus> {
|
||||
when (it) {
|
||||
LoginStatus.LOGIN_STATUS_NET_LOADING -> {
|
||||
loginDialog("验证用户信息...")
|
||||
}
|
||||
LoginStatus.LOGIN_STATUS_FOLDER_INIT -> {
|
||||
loginDialog("检查本地数据...")
|
||||
}
|
||||
LoginStatus.LOGIN_STATUS_FOLDER_FAILURE -> {
|
||||
Toast.makeText(this, "文件夹初始化失败", Toast.LENGTH_SHORT).show()
|
||||
loginDialog?.dismiss()
|
||||
loginDialog = null
|
||||
}
|
||||
LoginStatus.LOGIN_STATUS_NET_FAILURE -> {
|
||||
Toast.makeText(this, "网络访问失败", Toast.LENGTH_SHORT).show()
|
||||
loginDialog?.dismiss()
|
||||
loginDialog = null
|
||||
}
|
||||
LoginStatus.LOGIN_STATUS_SUCCESS -> {
|
||||
val intent = Intent(this@LoginActivity, MainActivity::class.java)
|
||||
startActivity(intent)
|
||||
// finish()
|
||||
loginDialog?.dismiss()
|
||||
loginDialog = null
|
||||
}
|
||||
LoginStatus.LOGIN_STATUS_CANCEL -> {
|
||||
loginDialog?.dismiss()
|
||||
loginDialog = null
|
||||
}
|
||||
loginDialog?.dismiss()
|
||||
loginDialog = null
|
||||
}
|
||||
LoginStatus.LOGIN_STATUS_CANCEL -> {
|
||||
loginDialog?.dismiss()
|
||||
loginDialog = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
//登录校验,初始化成功
|
||||
viewModel.loginStatus.observe(this, loginObserve)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录dialog
|
||||
*/
|
||||
@@ -73,7 +82,7 @@ class LoginActivity : PermissionsActivity() {
|
||||
loginDialog = MaterialAlertDialogBuilder(
|
||||
this, com.google.android.material.R.style.MaterialAlertDialog_Material3
|
||||
).setTitle("登录").setMessage(message).show()
|
||||
loginDialog!!.setCanceledOnTouchOutside(true)
|
||||
loginDialog!!.setCanceledOnTouchOutside(false)
|
||||
loginDialog!!.setOnCancelListener {
|
||||
viewModel.cancelLogin()
|
||||
}
|
||||
@@ -84,13 +93,17 @@ class LoginActivity : PermissionsActivity() {
|
||||
|
||||
//进应用根本不调用,待查
|
||||
override fun onPermissionsGranted() {
|
||||
Log.e("jingo","调用了吗")
|
||||
Log.e("jingo", "调用了吗")
|
||||
|
||||
}
|
||||
|
||||
override fun onPermissionsDenied() {
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理登录按钮
|
||||
*/
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.util.Log
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.navinfo.omqs.Constant
|
||||
@@ -65,7 +66,7 @@ class LoginViewModel(
|
||||
*/
|
||||
fun onClick(view: View) {
|
||||
loginUser.value!!.username = "admin2"
|
||||
loginUser.postValue(loginUser.value)
|
||||
loginUser.value = loginUser.value
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,7 +82,6 @@ class LoginViewModel(
|
||||
//不指定IO,会在主线程里运行
|
||||
jobLogin = viewModelScope.launch(Dispatchers.IO) {
|
||||
loginCheck(context, userName, password)
|
||||
Log.e("jingo", "运行完了1?${Thread.currentThread().name}")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,14 +90,12 @@ class LoginViewModel(
|
||||
*/
|
||||
|
||||
private suspend fun loginCheck(context: Context, userName: String, password: String) {
|
||||
Log.e("jingo", "我在哪个线程里?${Thread.currentThread().name}")
|
||||
//上面调用了线程切换,这里不用调用,即使调用了还是在同一个线程中,除非自定义协程域?(待验证)
|
||||
// withContext(Dispatchers.IO) {
|
||||
Log.e("jingo", "delay之前?${Thread.currentThread().name}")
|
||||
//网络访问
|
||||
loginStatus.postValue(LoginStatus.LOGIN_STATUS_NET_LOADING)
|
||||
//假装网络访问,等待3秒
|
||||
delay(3000)
|
||||
//假装网络访问,等待2秒
|
||||
delay(1000)
|
||||
//文件夹初始化
|
||||
try {
|
||||
loginStatus.postValue(LoginStatus.LOGIN_STATUS_FOLDER_INIT)
|
||||
@@ -108,7 +106,6 @@ class LoginViewModel(
|
||||
//假装解压文件等
|
||||
delay(1000)
|
||||
loginStatus.postValue(LoginStatus.LOGIN_STATUS_SUCCESS)
|
||||
Log.e("jingo", "delay之后?${Thread.currentThread().name}")
|
||||
|
||||
// }
|
||||
}
|
||||
@@ -121,6 +118,7 @@ class LoginViewModel(
|
||||
sdCardPath?.let {
|
||||
Constant.ROOT_PATH = sdCardPath.absolutePath
|
||||
Constant.MAP_PATH = Constant.ROOT_PATH + "/map/"
|
||||
Constant.OFFLINE_MAP_PATH = Constant.MAP_PATH + "offline/"
|
||||
val file = File(Constant.MAP_PATH)
|
||||
if (!file.exists()) {
|
||||
file.mkdirs()
|
||||
@@ -135,7 +133,7 @@ class LoginViewModel(
|
||||
Log.e("jingo", "取消了?${Thread.currentThread().name}")
|
||||
jobLogin?.let {
|
||||
it.cancel()
|
||||
loginStatus.postValue(LoginStatus.LOGIN_STATUS_CANCEL)
|
||||
loginStatus.value = LoginStatus.LOGIN_STATUS_CANCEL
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.navinfo.collect.library.map.NIMapController
|
||||
import com.navinfo.omqs.Constant
|
||||
import com.navinfo.omqs.R
|
||||
import com.navinfo.omqs.databinding.ActivityMainBinding
|
||||
import com.navinfo.omqs.http.offlinemapdownload.OfflineMapDownloadManager
|
||||
import com.navinfo.omqs.ui.activity.BaseActivity
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
@@ -27,6 +28,8 @@ class MainActivity : BaseActivity() {
|
||||
//注入地图控制器
|
||||
@Inject
|
||||
lateinit var mapController: NIMapController
|
||||
@Inject
|
||||
lateinit var offlineMapDownloadManager: OfflineMapDownloadManager
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.navinfo.omqs.ui.fragment.offlinemap
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||
import dagger.hilt.EntryPoint
|
||||
|
||||
/**
|
||||
* 离线地图主页面,viewpage适配器
|
||||
|
||||
@@ -1,33 +1,118 @@
|
||||
package com.navinfo.omqs.ui.fragment.offlinemap
|
||||
|
||||
import androidx.databinding.ViewDataBinding
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.lifecycle.Observer
|
||||
import com.navinfo.omqs.R
|
||||
import com.navinfo.omqs.BR
|
||||
import com.navinfo.omqs.bean.OfflineMapCityBean
|
||||
import com.navinfo.omqs.databinding.AdapterOfflineMapCityBinding
|
||||
import com.navinfo.omqs.http.offlinemapdownload.OfflineMapDownloadManager
|
||||
import com.navinfo.omqs.ui.other.BaseRecyclerViewAdapter
|
||||
import com.navinfo.omqs.ui.other.BaseViewHolder
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* 离线地图城市列表 RecyclerView 适配器
|
||||
*
|
||||
* 在 RecycleView 的 ViewHolder 中监听 ViewModel 的 LiveData,然后此时传递的 lifecycleOwner 是对应的 Fragment。由于 ViewHolder 的生命周期是比 Fragment 短的,所以当 ViewHolder 销毁时,由于 Fragment 的 Lifecycle 还没有结束,此时 ViewHolder 会发生内存泄露(监听的 LiveData 没有解绑)
|
||||
* 这种场景下有两种解决办法:
|
||||
*使用 LiveData 的 observeForever 然后在 ViewHolder 销毁前手动调用 removeObserver
|
||||
*使用 LifecycleRegistry 给 ViewHolder 分发生命周期(这里使用了这个)
|
||||
*/
|
||||
class OfflineMapCityListAdapter @Inject constructor(
|
||||
private val downloadManager: OfflineMapDownloadManager, private val context: Context
|
||||
) : BaseRecyclerViewAdapter<OfflineMapCityBean>() {
|
||||
|
||||
class OfflineMapCityListAdapter @Inject constructor() :
|
||||
BaseRecyclerViewAdapter<OfflineMapCityBean>() {
|
||||
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
|
||||
var binding: ViewDataBinding = holder.dataBinding
|
||||
//立刻刷新UI,解决闪烁
|
||||
// binding.executePendingBindings()
|
||||
binding.setVariable(BR.cityBean, data[position])
|
||||
(binding as AdapterOfflineMapCityBinding).offlineMapDownloadBtn.setOnClickListener {
|
||||
|
||||
private val downloadBtnClick = View.OnClickListener() {
|
||||
if (it.tag != null) {
|
||||
val cityBean = data[it.tag as Int]
|
||||
when (cityBean.status) {
|
||||
OfflineMapCityBean.NONE, OfflineMapCityBean.UPDATE, OfflineMapCityBean.PAUSE, OfflineMapCityBean.ERROR -> {
|
||||
downloadManager.start(cityBean.id)
|
||||
}
|
||||
OfflineMapCityBean.LOADING, OfflineMapCityBean.WAITING -> {
|
||||
downloadManager.pause(cityBean.id)
|
||||
}
|
||||
// OfflineMapCityBean.WAITING->{
|
||||
// downloadManager.cancel(cityBean.id)
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
|
||||
val viewBinding =
|
||||
AdapterOfflineMapCityBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
||||
return BaseViewHolder(viewBinding)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
|
||||
val binding: AdapterOfflineMapCityBinding =
|
||||
holder.viewBinding as AdapterOfflineMapCityBinding
|
||||
//牺牲性能立刻刷新UI,解决闪烁 这里不用
|
||||
// binding.executePendingBindings()
|
||||
val cityBean = data[position]
|
||||
binding.offlineMapDownloadBtn.tag = position
|
||||
binding.offlineMapDownloadBtn.setOnClickListener(downloadBtnClick)
|
||||
binding.offlineMapCityName.text = cityBean.name
|
||||
binding.offlineMapCitySize.text = cityBean.getFileSizeText()
|
||||
downloadManager.addTask(cityBean)
|
||||
changeViews(binding, cityBean)
|
||||
downloadManager.observer(cityBean.id, holder) {
|
||||
if (cityBean.id == it.id)
|
||||
changeViews(binding, it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun changeViews(binding: AdapterOfflineMapCityBinding, cityBean: OfflineMapCityBean) {
|
||||
binding.offlineMapProgress.progress =
|
||||
(cityBean.currentSize * 100 / cityBean.fileSize).toInt()
|
||||
when (cityBean.status) {
|
||||
OfflineMapCityBean.NONE -> {
|
||||
if (binding.offlineMapProgress.visibility == View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.INVISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "下载"
|
||||
}
|
||||
OfflineMapCityBean.WAITING -> {
|
||||
if (binding.offlineMapProgress.visibility != View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "等待中"
|
||||
}
|
||||
OfflineMapCityBean.LOADING -> {
|
||||
if (binding.offlineMapProgress.visibility != View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "暂停"
|
||||
}
|
||||
OfflineMapCityBean.PAUSE -> {
|
||||
if (binding.offlineMapProgress.visibility != View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "继续"
|
||||
}
|
||||
OfflineMapCityBean.ERROR -> {
|
||||
if (binding.offlineMapProgress.visibility != View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.VISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "重试"
|
||||
}
|
||||
OfflineMapCityBean.DONE -> {
|
||||
if (binding.offlineMapProgress.visibility == View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.INVISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "已完成"
|
||||
}
|
||||
OfflineMapCityBean.UPDATE -> {
|
||||
if (binding.offlineMapProgress.visibility == View.VISIBLE) binding.offlineMapProgress.visibility =
|
||||
View.INVISIBLE
|
||||
binding.offlineMapDownloadBtn.text = "更新"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return R.layout.adapter_offline_map_city
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -10,17 +10,28 @@ import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.navinfo.omqs.databinding.FragmentOfflineMapCityListBinding
|
||||
import com.navinfo.omqs.http.RetrofitNetworkServiceAPI
|
||||
import com.navinfo.omqs.http.offlinemapdownload.OfflineMapDownloadManager
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* 离线地图城市列表
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class OfflineMapCityListFragment : Fragment() {
|
||||
@Inject
|
||||
lateinit var downloadManager: OfflineMapDownloadManager
|
||||
private var _binding: FragmentOfflineMapCityListBinding? = null
|
||||
private val viewModel by viewModels<OfflineMapCityListViewModel>()
|
||||
private val binding get() = _binding!!
|
||||
private val adapter: OfflineMapCityListAdapter by lazy { OfflineMapCityListAdapter() }
|
||||
private val adapter: OfflineMapCityListAdapter by lazy {
|
||||
OfflineMapCityListAdapter(
|
||||
downloadManager,
|
||||
requireContext()
|
||||
)
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
@@ -33,8 +44,10 @@ class OfflineMapCityListFragment : Fragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
val layoutManager = LinearLayoutManager(context)
|
||||
_binding!!.offlineMapCityListRecyclerview.layoutManager = layoutManager
|
||||
_binding!!.offlineMapCityListRecyclerview.adapter = adapter
|
||||
//// 设置 RecyclerView 的固定大小,避免在滚动时重新计算视图大小和布局,提高性能
|
||||
binding.offlineMapCityListRecyclerview.setHasFixedSize(true)
|
||||
binding.offlineMapCityListRecyclerview.layoutManager = layoutManager
|
||||
binding.offlineMapCityListRecyclerview.adapter = adapter
|
||||
viewModel.cityListLiveData.observe(viewLifecycleOwner) {
|
||||
adapter.refreshData(it)
|
||||
}
|
||||
@@ -44,6 +57,6 @@ class OfflineMapCityListFragment : Fragment() {
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
Log.e("jingo","OfflineMapCityListFragment onDestroyView")
|
||||
Log.e("jingo", "OfflineMapCityListFragment onDestroyView")
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ class OfflineMapCityListViewModel @Inject constructor(
|
||||
viewModelScope.launch {
|
||||
when (val result = networkService.getOfflineMapCityList()) {
|
||||
is NetResult.Success -> {
|
||||
cityListLiveData.postValue(result.data!!)
|
||||
cityListLiveData.postValue(result.data?.sortedBy { bean -> bean.id })
|
||||
}
|
||||
is NetResult.Error -> {
|
||||
Toast.makeText(context, "${result.exception.message}", Toast.LENGTH_SHORT)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.navinfo.omqs.ui.fragment.offlinemap
|
||||
|
||||
import com.navinfo.omqs.databinding.AdapterOfflineMapCityBinding
|
||||
import com.navinfo.omqs.ui.other.BaseViewHolder
|
||||
|
||||
class OfflineMapViewHolder(dataBinding: AdapterOfflineMapCityBinding) : BaseViewHolder(dataBinding) {
|
||||
init{
|
||||
dataBinding.offlineMapDownloadBtn
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.navinfo.omqs.ui.other
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View.OnClickListener
|
||||
import android.view.ViewGroup
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
@@ -10,23 +11,51 @@ import androidx.recyclerview.widget.RecyclerView
|
||||
*/
|
||||
abstract class BaseRecyclerViewAdapter<T>(var data: List<T> = listOf()) :
|
||||
RecyclerView.Adapter<BaseViewHolder>() {
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
|
||||
return BaseViewHolder(
|
||||
DataBindingUtil.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
viewType,
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
}
|
||||
// private var recyclerView: RecyclerView? = null
|
||||
// override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
|
||||
//
|
||||
//
|
||||
//
|
||||
// return BaseViewHolder(
|
||||
// DataBindingUtil.inflate(
|
||||
// LayoutInflater.from(parent.context),
|
||||
// viewType,
|
||||
// parent,
|
||||
// false
|
||||
// )
|
||||
// )
|
||||
// }
|
||||
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return data.size
|
||||
}
|
||||
|
||||
fun refreshData(newData:List<T>){
|
||||
fun refreshData(newData: List<T>) {
|
||||
this.data = newData
|
||||
this.notifyDataSetChanged()
|
||||
}
|
||||
|
||||
|
||||
override fun onViewAttachedToWindow(holder: BaseViewHolder) {
|
||||
super.onViewAttachedToWindow(holder)
|
||||
holder.onStart()
|
||||
}
|
||||
|
||||
override fun onViewDetachedFromWindow(holder: BaseViewHolder) {
|
||||
super.onViewDetachedFromWindow(holder)
|
||||
holder.apply {
|
||||
onStop()
|
||||
}
|
||||
}
|
||||
//
|
||||
// override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
|
||||
// super.onAttachedToRecyclerView(recyclerView)
|
||||
// this.recyclerView = recyclerView
|
||||
// }
|
||||
//
|
||||
// override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
|
||||
// super.onDetachedFromRecyclerView(recyclerView)
|
||||
// this.recyclerView = null
|
||||
// }
|
||||
}
|
||||
@@ -1,11 +1,54 @@
|
||||
package com.navinfo.omqs.ui.other
|
||||
|
||||
import androidx.databinding.ViewDataBinding
|
||||
import android.view.View
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.LifecycleRegistry
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.viewbinding.ViewBinding
|
||||
|
||||
/**
|
||||
* dataBinding viewHolder 基类
|
||||
* LifecycleRegistry 这是一个生命周期注册器,继承自 Lifecycle,LifecycleOwner 通过这个类来分发生命周期事件,并在 getLifecycle() 中返回
|
||||
*/
|
||||
open class BaseViewHolder(var dataBinding: ViewDataBinding) :
|
||||
RecyclerView.ViewHolder(dataBinding.root) {
|
||||
open class BaseViewHolder(val viewBinding: ViewBinding) :
|
||||
RecyclerView.ViewHolder(viewBinding.root), LifecycleOwner {
|
||||
private val lifecycleRegistry = LifecycleRegistry(this)
|
||||
|
||||
init {
|
||||
// dataBinding.lifecycleOwner = this
|
||||
lifecycleRegistry.currentState = Lifecycle.State.INITIALIZED
|
||||
lifecycleRegistry.currentState = Lifecycle.State.CREATED
|
||||
itemView.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
|
||||
// View onDetached 的时候回调 onDestroy()
|
||||
override fun onViewDetachedFromWindow(v: View) {
|
||||
itemView.removeOnAttachStateChangeListener(this)
|
||||
onDestroy()
|
||||
}
|
||||
|
||||
// View onAttached 的时候回调 onCreate()
|
||||
override fun onViewAttachedToWindow(v: View) {
|
||||
onStart()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun onStart() {
|
||||
lifecycleRegistry.currentState = Lifecycle.State.STARTED //
|
||||
lifecycleRegistry.currentState = Lifecycle.State.RESUMED // ON_RESUME EVENT
|
||||
}
|
||||
|
||||
fun onStop() {
|
||||
lifecycleRegistry.currentState = Lifecycle.State.STARTED //
|
||||
lifecycleRegistry.currentState = Lifecycle.State.CREATED // ON_STOP EVENT
|
||||
}
|
||||
|
||||
fun onDestroy() {
|
||||
lifecycleRegistry.currentState = Lifecycle.State.DESTROYED /// ON_DESTROY EVENT
|
||||
}
|
||||
|
||||
|
||||
override fun getLifecycle(): Lifecycle {
|
||||
return lifecycleRegistry
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,13 @@ import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.Paint
|
||||
import android.graphics.Rect
|
||||
import android.opengl.ETC1.getHeight
|
||||
import android.opengl.ETC1.getWidth
|
||||
import android.util.AttributeSet
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.ProgressBar
|
||||
import com.navinfo.omqs.R
|
||||
|
||||
|
||||
/**
|
||||
@@ -18,8 +21,13 @@ class MyProgressBar : ProgressBar {
|
||||
private lateinit var mPaint: Paint
|
||||
private var text: String = ""
|
||||
private var rate = 0f
|
||||
private lateinit var bar: ProgressBar
|
||||
|
||||
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
|
||||
// LayoutInflater.from(context).inflate(
|
||||
// R.layout.my_projressbar, this,
|
||||
// true
|
||||
// );
|
||||
initView()
|
||||
}
|
||||
|
||||
@@ -33,6 +41,7 @@ class MyProgressBar : ProgressBar {
|
||||
mPaint.color = Color.BLUE
|
||||
}
|
||||
|
||||
|
||||
@Synchronized
|
||||
override fun setProgress(progress: Int) {
|
||||
setText(progress)
|
||||
@@ -40,7 +49,7 @@ class MyProgressBar : ProgressBar {
|
||||
}
|
||||
|
||||
private fun setText(progress: Int) {
|
||||
rate = progress * 1.0f / this.getMax()
|
||||
rate = progress * 1.0f / this.max
|
||||
val i = (rate * 100).toInt()
|
||||
text = "$i%"
|
||||
}
|
||||
@@ -57,8 +66,9 @@ class MyProgressBar : ProgressBar {
|
||||
// 如果为百分之百则在左边绘制。
|
||||
x = width - rect.right
|
||||
}
|
||||
val y: Int = 0 - rect.top
|
||||
mPaint.textSize = 22f
|
||||
mPaint.textSize = 24f
|
||||
val y: Int = 10 - rect.top
|
||||
|
||||
canvas.drawText(text, x.toFloat(), y.toFloat(), mPaint)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user