相关疑难解决方法(0)

ViewPager2 的 FragmentStateAdapter notifyItemChanged 未按预期工作

我正在使用带有 FragmentStateAdapter 的 ViewPager2,我正在调用 notifyItemChanged(position)。但正如预期的那样 createFragment 方法不会再次调用。这是预期的行为还是错误,我该怎么办

我的代码是

    class EEditionPagerAdapter(
    fragmentManager: FragmentManager, lifecycle: Lifecycle, private var date: String
    ) : FragmentStateAdapter(fragmentManager, lifecycle) {

    private var menuList = emptyList<EEditionMenu>()

    override fun getItemCount(): Int = menuList.size

    override fun createFragment(position: Int): Fragment {
        val menu = menuList[position]
        return EEditionListingFragment.newInstance(menu, date)
    }

    fun submitList(list: List<EEditionMenu>) {
        menuList = list
        notifyItemRangeChanged(0, menuList.size)
    }

    fun changeDate(date: String, position: Int){
        this.date = date
        notifyItemChanged(position)
    }
}
Run Code Online (Sandbox Code Playgroud)

android android-viewpager2

6
推荐指数
2
解决办法
2655
查看次数

标签 统计

android ×1

android-viewpager2 ×1