Search code examples
androidkotlinandroid-viewpager2

How can I create Fragments dynamically in viewPager according to boolean values?


These codes are working. I create Fragment according these boolean values are false or true. But I want to do this with less code. I just tried add them in for loops but it gets longer also. How can I create fragment here dynamically with less code? I can't find a solution in stackOverflow and on the internet.

    class ReportASMViewPagerAdapter(
        fragmentManager: FragmentManager,
        lifecycle: Lifecycle,
        private val visitId: String,
        private val facilityId: String,
        private val tabItemCount: Int,
        private val hasLabResult: Boolean,
        private val hasRadResult: Boolean
    ) : FragmentStateAdapter(fragmentManager, lifecycle) {
        override fun getItemCount(): Int {
            return tabItemCount
        }
    
        override fun createFragment(position: Int): Fragment {
            
            if (hasLabResult && hasRadResult) {
                return when (position) {
                    0 -> {
                        LaboratoryASMFragment.newInstance(referenceVisitId = visitId, facilityId = facilityId)
                    }
                    1 -> {
                        RadiologyASMFragment.newInstance(referenceVisitId = visitId, facilityId = facilityId)
                    }
                    2 -> {
                        PrescriptionASMFragment.newInstance(referenceVisitId = visitId, facilityId = facilityId)
                    }
                    3 -> {
                        OtherReportsASMFragment()
                    }
                    else -> {
                        Fragment()
                    }
                }
            } else if (hasLabResult && !hasRadResult) {
                return when (position) {
                    0 -> {
                        LaboratoryASMFragment.newInstance(referenceVisitId = visitId, facilityId = facilityId)
                    }
                    1 -> {
                        PrescriptionASMFragment.newInstance(referenceVisitId = visitId, facilityId = facilityId)
                    }
                    2 -> {
                        OtherReportsASMFragment()
                    }
                    else -> {
                        Fragment()
                    }
                }
            } else {
                return when (position) {
                    0 -> {
                        RadiologyASMFragment.newInstance(referenceVisitId = visitId, facilityId = facilityId)
                    }
                    1 -> {
                        PrescriptionASMFragment.newInstance(referenceVisitId = visitId, facilityId = facilityId)
                    }
                    2 -> {
                        OtherReportsASMFragment()
                    }
                    else -> {
                        Fragment()
                    }
                }
            }
        }
    }

Solution

  • You can use bellow codes but I think your codes are more readable:

     override fun createFragment(position: Int): Fragment {
    
            var newPosition = position
    
            if (hasLabResult && hasRadResult) {
                newPosition = position
            } else if (hasLabResult && !hasRadResult) {
                if (position >= 1) newPosition = position + 1
            } else {
                if (position != 0) newPosition = position + 1
            }
    
            return when (newPosition) {
                0 -> LaboratoryASMFragment.newInstance(referenceVisitId = visitId, facilityId = facilityId)
                1 -> RadiologyASMFragment.newInstance(referenceVisitId = visitId, facilityId = facilityId)
                2 -> PrescriptionASMFragment.newInstance(referenceVisitId = visitId, facilityId = facilityId)
                3 -> OtherReportsASMFragment()
                else -> Fragment()
            }
            
        }