自定义PagerAdapter
在Activity使用
val data = ArrayList()
data.add(view)
val viewAdapter = ViewAdapter(data)
viewPager.adapter = viewAdapter
自定义PagerAdapter源代码
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
class ViewAdapter(val data: ArrayList) : PagerAdapter() {
override fun getCount(): Int {
return data.size
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
container.addView(data[position])
return data[position]
}
override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
container.removeView(data[position])
}
override fun isViewFromObject(view: View, obj: Any): Boolean {
return view == obj
}
}
网友评论