UI反复设置-数据绑定,MVVM,导航组件

问题描述 投票:2回答:1

我在当前项目中使用数据绑定和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

更新:我将计划获取逻辑移到了onResume上,该逻辑处理了UI的闪烁。但是,当用户返回屏幕时,适配器将再次设置。这与片段生命周期有关,因此我认为没有办法解决。如果我错了,请纠正我。

override fun onResume() {
        super.onResume()
        savingPlanViewModel.getAllSavingPlan()
 }

附加说明:我正在使用android导航组件。

片段本身是其XML中带有viewpager的父片段的一部分。 viewpager是活动/非活动计划子片段的主机。父类视图寻呼机的屏幕外页面限制设置为2。

savingPlanViewPager.adapter = SavingPlanViewPagerAdapter(childFragmentManager)
savingPlanViewPager.offscreenPageLimit = 2
savingPlanTabLayout.setupWithViewPager(savingPlanViewPager)
android mvvm android-databinding android-livedata android-architecture-navigation
1个回答
0
投票

我最终使用惰性初始化仅将适配器设置一次。如果有人在寻找,这是解决方案:

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必然会一遍又一遍地触发,但是,如上面的代码所示,我们可以通过惰性控制适配器和数据初始化。

© www.soinside.com 2019 - 2024. All rights reserved.