Just today I am trying to build an app module in Android Studio, with Kotlin 1.1.51. This code to implement PagerAdapter has compiled for a long time, but suddenly it is getting confused with overriding function parameter and/or return types of Java interface.
internal inner class RegistrationPagerAdapter : PagerAdapter(), ViewPager.OnPageChangeListener {
var expanded: Boolean = false
set(value) {
if (field == value)
return
field = value
notifyDataSetChanged()
this@LoginActivity.pageIndicator.notifyDataSetChanged()
}
val layouts = arrayOf(R.layout.fragment_login, R.layout.fragment_reg_1, R.layout.fragment_reg_2,
R.layout.fragment_reg_3, R.layout.fragment_reg_4)
override fun instantiateItem(container: ViewGroup, position: Int): ViewDataBinding? {
val layout = layouts[position]
return when (layout) {
R.layout.fragment_login -> this@LoginActivity.setupLogin(container)
R.layout.fragment_reg_1 -> this@LoginActivity.setupPage1(container)
R.layout.fragment_reg_2 -> this@LoginActivity.setupPage2(container)
R.layout.fragment_reg_3 -> this@LoginActivity.setupPage3(container)
R.layout.fragment_reg_4 -> this@LoginActivity.setupPage4(container)
else -> throw IllegalStateException("unknown item in RegistrationPagerAdapter")
}
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
when (layouts[position]) {
R.layout.fragment_login -> this@LoginActivity.loginBinding = null
R.layout.fragment_reg_1 -> this@LoginActivity.reg1Binding = null
R.layout.fragment_reg_2 -> this@LoginActivity.reg2Binding = null
R.layout.fragment_reg_3 -> this@LoginActivity.reg3Binding = null
R.layout.fragment_reg_4 -> this@LoginActivity.reg4Binding = null
}
container.removeView((`object` as ViewDataBinding).root)
}
override fun isViewFromObject(view: View?, `object`: Any): Boolean {
if (`object` === this@LoginActivity.loginBinding)
return view === this@LoginActivity.loginBinding?.root
if (`object` === this@LoginActivity.reg1Binding)
return view === this@LoginActivity.reg1Binding?.root
if (`object` === this@LoginActivity.reg2Binding)
return view === this@LoginActivity.reg2Binding?.root
if (`object` === this@LoginActivity.reg3Binding)
return view === this@LoginActivity.reg3Binding?.root
if (`object` === this@LoginActivity.reg4Binding)
return view === this@LoginActivity.reg4Binding?.root
throw IllegalStateException("unknown item in RegistrationPagerAdapter")
}
override fun restoreState(state: Parcelable, loader: ClassLoader?) {
this.expanded = Parcels.unwrap(state)
super.restoreState(state, loader)
}
...
And now, I suddenly have errors
e: (1072, 20): Class ‘RegistrationPagerAdapter’ is not abstract and does not implement abstract base class member public abstract fun isViewFromObject(@NonNull p0: View, @NonNull p1: Any): Boolean defined in android.support.v4.view.PagerAdapter
e: (1085, 76): Return type of ‘instantiateItem’ is not a subtype of the return type of the overridden member ‘@NonNull public open fun instantiateItem(@NonNull p0: ViewGroup, p1: Int): Any defined in android.support.v4.view.PagerAdapter’
e: (1108, 9): ‘isViewFromObject’ overrides nothing
e: (1155, 9): ‘restoreState’ overrides nothing
The IDE doesn’t show an error in the open source file. I really can’t imagine why these would suddenly start to appear. IDE has Kotlin plugin of same version as dependency in build.gradle