如何根据布尔值在 viewPager 中动态创建片段?
How can I create Fragments dynamically in viewPager according to boolean values?
这些代码有效。我根据这些布尔值是 false 还是 true 创建 Fragment。但我想用更少的代码来做到这一点。我只是尝试将它们添加到 for 循环中,但它也变得更长了。如何使用更少的代码在此处动态创建片段?我在 Whosebug 和互联网上都找不到解决方案。
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()
}
}
}
}
}
您可以使用以下代码,但我认为您的代码更具可读性:
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()
}
}
这些代码有效。我根据这些布尔值是 false 还是 true 创建 Fragment。但我想用更少的代码来做到这一点。我只是尝试将它们添加到 for 循环中,但它也变得更长了。如何使用更少的代码在此处动态创建片段?我在 Whosebug 和互联网上都找不到解决方案。
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()
}
}
}
}
}
您可以使用以下代码,但我认为您的代码更具可读性:
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()
}
}