在我的应用程序中,显示了月历列表,可以滚动到上个月和下个月。当滑动到上一个和下一个时,如果我使用 FragmentStatePagerAdapter 和 ViewPager,我的片段会顺利加载,没有任何延迟,但它现在已被弃用。所以我更新了
ViewPager 到 ViewPager2
FragmentStatePagerAdapter 到 FragmentStateAdapter
更新后,我可以看到在滑动上一个和下一个片段时加载片段有延迟。但相同的代码可以在 FragmentStatePagerAdapter 和 ViewPager 中顺利运行。 我也尝试过
mViewPager2.setOffscreenPageLimit(//Interger Value//)
但没有帮助。
请帮助我。
fragment_employee_months_holder
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/fragment_months_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:backgroundTint="@color/colorPrimary" />
</RelativeLayout>
EmployeeSummaryAdapter
class EmployeeSummaryAdapter(
fragment: Fragment,
private val mCodes: List<String>,
private val mListener: NavigationListener,
private val progressBarInterface: ProgressBarInterface
) : FragmentStateAdapter(fragment) {
private val mFragments = SparseArray<EmployeeSummaryFragment>()
override fun createFragment(position: Int): EmployeeSummaryFragment {
val bundle = Bundle()
val code = mCodes[position]
bundle.putString(DAY_CODE, code)
val fragment = EmployeeSummaryFragment(progressBarInterface)
fragment.arguments = bundle
fragment.listener = mListener
mFragments.put(position, fragment)
return fragment
}
override fun getItemCount(): Int = mCodes.size
override fun getItemId(position: Int): Long {
return position.toLong()
}
}
EmployeeSummaryFragmentHolder
class EmployeeSummaryFragmentHolder : MyFragmentHolder(), NavigationListener, ProgressBarInterface {
private var viewPager2: ViewPager2? = null
private lateinit var mEmployeeSummaryAdapter: EmployeeSummaryAdapter
private lateinit var mView: View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
currentDayCode = Formatter.getTodayCode()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
mView = inflater.inflate(R.layout.fragment_employee_months_holder, container, false)
viewPager2 = mView.fragment_months_viewpager
viewPager2!!.id = (System.currentTimeMillis() % 100000).toInt()
Handler(Looper.getMainLooper()).postDelayed({
setupFragment()
}, 0)
return mView
}
private fun setupFragment() {
val codes = getMonths(currentDayCode)
mEmployeeSummaryAdapter = EmployeeSummaryAdapter(this, codes, this, this)
viewPager2.apply {
viewPager2!!.adapter = mEmployeeSummaryAdapter
val myPageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
override fun onPageScrollStateChanged(state: Int) { }
override fun onPageScrolled(position: Int, positionOffset: Float,
positionOffsetPixels: Int) { }
override fun onPageSelected(position: Int) { }
}
viewPager2!!.registerOnPageChangeCallback(myPageChangeCallback)
}
}
}
EmployeeSummaryFragment
class EmployeeSummaryFragment(val progressBarInterface: ProgressBarInterface) :
Fragment(), MonthlyCalendar {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_employee_month, container, false)
...
...
return view
}
override fun onResume() {
super.onResume()
...
...
}
}
我尝试查看您的代码,但没有发现任何可疑内容,但您绝对需要将
mViewPager2.setOffscreenPageLimit(//Interger Value//)
设置为 EmployeeSummaryFragmentHolder
。
viewpager2.offscreenPageLimit = 2
如果你能检查
mView
是否已经初始化,那就更优化了。
您可以简单地添加以下内容:
if(mView == null){
mView = inflater.inflate(R.layout.fragment_employee_months_holder, container, false)
}