android - UI 重复设置 - 数据绑定、MVVM、导航组件
问题描述
我在当前项目中使用数据绑定和 MVVM。这是我正在处理的屏幕之一的代码:
class ActivePlansFragment : Fragment() {
private lateinit var savingPlanViewModel: SavingPlanViewModel
private var isFinancialFreedomPlanOpted = false
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
printLog("On Create View")
return inflater.inflate(R.layout.fragment_active_plans, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
savingPlanViewModel = activityViewModels {
printLog("Setting Adapter For Active plans")
rvAllPlans.apply {
adapter = ActivePlansAdapter().apply {
mBaseAdapterClickListener = { view, position, item ->
if( view.id == R.id.tvAction ) {
if( isCurrentDestination(R.id.savingPlanListFragment) ) {
selectedNeoSavingPlan = item
findNavController().navigate(SavingPlanListFragmentDirections.actionSavingPlanListFragmentToAddFundsFragment())
}
} else {
if( isCurrentDestination(R.id.savingPlanListFragment) ) {
selectedNeoSavingPlan = item
findNavController().navigate(SavingPlanListFragmentDirections.actionSavingPlanListFragmentToSavingPlanDetailsFragment())
}
}
}
onEmptyOrNot = { isEmpty ->
emptyPlansLayout.changeVisibility(isEmpty)
}
//Update : getAllSavingPlan()
observe(allSavingPlanResponsesMutableLiveData) {
it?.onChanged { inProgress, failure, list ->
//fabCreatePlan.isEnabled = !inProgress
planRefreshLayout.isRefreshing = inProgress
if (!inProgress)
if (failure != null) {
handleFailures(failure) { getAllSavingPlan() }
allSavingPlanResponsesMutableLiveData.clearValue()
} else if (list != null) {
removeAll()
val filteredList = list.filter { plan ->
plan.active
|| plan.status == NeoSavingPlanResponse.COMPLETED
|| plan.status == NeoSavingPlanResponse.CREATE_INITIATED
|| plan.status == NeoSavingPlanResponse.DELETE_INITIATED
}
addAll(filteredList)
isFinancialFreedomPlanOpted = list.find { plan -> plan.planType == NeoMasterSavingPlan.FINANCIAL_FREEDOM_PLAN } != null
}
}
}
}
}
planRefreshLayout.setOnRefreshListener {
getAllSavingPlan()
}
}
}
}
我面临的问题是适配器,当我导航回屏幕时,其余的逻辑又被重新设置了。我正在寻找有关在何处设置 UI 逻辑以避免重置 UI 的帮助。
> Logcat logs :
>
> D/ActivePlansFragment: On Create View
> D/ActivePlansFragment: Setting Adapter For Active plans
> D/ActivePlansFragment: On Create View
> D/ActivePlansFragment: Setting Adapter For Active plans
更新:我将计划获取逻辑移至处理 UI 闪烁的 onResume。但是当用户返回屏幕时,适配器会再次设置。这与片段生命周期有关,所以我认为没有办法解决这个问题。如果我错了,请纠正我。
override fun onResume() {
super.onResume()
savingPlanViewModel.getAllSavingPlan()
}
附加说明:我使用的是 android 导航组件。
片段本身是其 xml 中带有 viewpager 的父片段的一部分。viewpager 是活动/非活动计划子片段的主机。父类视图寻呼机的屏幕外页面限制设置为 2。
savingPlanViewPager.adapter = SavingPlanViewPagerAdapter(childFragmentManager)
savingPlanViewPager.offscreenPageLimit = 2
savingPlanTabLayout.setupWithViewPager(savingPlanViewPager)
解决方案
我最终使用延迟初始化来设置适配器一次。如果有人正在寻找它,这是解决方案:
class ActivePlansFragment : Fragment() {
private lateinit var savingPlanViewModel: SavingPlanViewModel
private var isFinancialFreedomPlanOpted = false
private val navController : NavController by lazy { findNavController() }
private val adapterClickListener : ((view : View, position : Int, item : NeoSavingPlanResponse) -> Unit) = { view, _, item ->
if( view.id == R.id.tvAction ) {
if( isCurrentDestination(R.id.savingPlanListFragment) ) {
savingPlanViewModel.selectedNeoSavingPlan = item
navController.navigate(SavingPlanListFragmentDirections.actionSavingPlanListFragmentToAddFundsFragment())
}
} else {
if( isCurrentDestination(R.id.savingPlanListFragment) ) {
savingPlanViewModel.selectedNeoSavingPlan = item
navController.navigate(SavingPlanListFragmentDirections.actionSavingPlanListFragmentToSavingPlanDetailsFragment())
}
}
}
private val onEmptyAdapterListener : ((isEmpty: Boolean) -> Unit) = {
isEmpty -> emptyPlansLayout.changeVisibility(isEmpty)
}
private val activePlansAdapter: ActivePlansAdapter by lazy {
ActivePlansAdapter().apply {
mBaseAdapterClickListener = adapterClickListener
onEmptyOrNot = onEmptyAdapterListener
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_active_plans, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
savingPlanViewModel = activityViewModels {
getAllSavingPlan()
}
rvAllPlans.apply { adapter = activePlansAdapter }
planRefreshLayout.setOnRefreshListener { savingPlanViewModel.getAllSavingPlan() }
setupObserver()
}
private fun setupObserver() {
savingPlanViewModel.apply {
observe(allSavingPlanResponsesMutableLiveData) {
it?.onChanged { inProgress, failure, list ->
planRefreshLayout.isRefreshing = inProgress
if (!inProgress)
if (failure != null) {
handleFailures(failure) { savingPlanViewModel.getAllSavingPlan() }
allSavingPlanResponsesMutableLiveData.clearValue()
} else if (list != null) {
activePlansAdapter.removeAll()
val filteredList = list.filter { plan ->
plan.active
|| plan.status == NeoSavingPlanResponse.COMPLETED
|| plan.status == NeoSavingPlanResponse.CREATE_INITIATED
|| plan.status == NeoSavingPlanResponse.DELETE_INITIATED
}
activePlansAdapter.addAll(filteredList)
isFinancialFreedomPlanOpted = list.find {
plan -> plan.planType == NeoMasterSavingPlan.FINANCIAL_FREEDOM_PLAN
} != null
}
}
}
}
}
}
onCreateView 必然会反复触发,但是我们可以通过惰性控制适配器和数据初始化,如上面的代码所示。
推荐阅读
- javascript - 如何将获取发布请求发送到基于 API 密钥的授权端点?
- command-line-interface - 替换文本->(如何)?
- sql - 根据数量和目标维度数据将数量拆分为多行
- javascript - 使用 Pika CDN 在路径下导入?
- csv - 使用 Neo4j 加载 csv 命令的困难
- java - 在 Docker 容器上运行 JAR 时写入文件夹中的文件会导致 FileNotFoundException
- java - 使用 Spring Boot 更新 MongoDB 对象
- visual-studio-code - 如何将“显示预览”按钮添加到 VS Code 扩展?
- amazon-web-services - 想要使用 JAVA 8 将 VPC 中的 AWS lambda 调用到同一区域和帐户中没有 VPC 的另一个 AWS lambda
- python-3.x - 我想在 Python 脚本中使用以下命令和 subprocess.run()