Pager-Adapter在第一次呼叫时不显示数据

时间:2018-11-15 12:45:44

标签: android kotlin android-viewpager

我有一个PagerAdapter,由于某种原因,它第一次创建时不会显示数据!它仅在第二次创建时显示。.

这是PagerAdapter:

class ViewPagerAdapter() : PagerAdapter() {
lateinit var context: Context
lateinit var data: Data

constructor(context: Context?, data: Data) : this() {
    this.context = context!!
    this.data = data
}

override fun getCount(): Int {
    return data.myArray.size
}

override fun isViewFromObject(view: View, `object`: Any): Boolean {
    return view == `object`
}

override fun instantiateItem(container: ViewGroup, position: Int): Any {

    val binding = DataItemBinding.inflate(LayoutInflater.from(context), container, false)
    binding.data = data
    binding.executePendingBindings()
    container.addView(binding.root)
    return binding.root
}

override fun getItemPosition(`object`: Any): Int {
    return PagerAdapter.POSITION_NONE
}

override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
    container.removeView(`object` as ConstraintLayout)
}}

这是活动中的适配器调用:

   override fun onStart() {
    super.onStart()
    val viewPagerAdapter = ViewPagerAdapter(this, data)
    viewPager.adapter = viewPagerAdapter
    viewPager.currentItem = data.myArray.lastIndex
    indicator.setViewPager(viewPager)

}

1 个答案:

答案 0 :(得分:0)

我认为您的Feed me text: coke 1000;180;56124;coke;sprite;water;juice 566;40;10000;cake;pizza;coke 方法导致了此问题。您应该像下面这样覆盖它:

isViewFromObject