я работаю на Android kotlin, когда создаю слайдер URL изображений с помощью ViewPager. Приложение закрывается. Мне нужно показывать изображения с URL для ViewPager. Мой код:
viewpageradaptor
class pagerview( private var context:Context,var url:ArrayList<String>?= null):PagerAdapter(){
private var layoutinflat:LayoutInflater?=null
// val images= arrayOf (R.drawable.abstract2, R.drawable.air)
override fun isViewFromObject(view: View, `object`: Any): Boolean {
обратный просмотр == object
}
override fun getCount(): Int {
return url!!.size
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
layoutinflat=context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var v=layoutinflat!!.inflate(R.layout.slide_swipe, container, false)
val image=v.findViewById<View>(R.id.imageslider) as ImageView
Glide.with(context).load(url).into(image)
val vp =container as ViewPager
vp.addView(v,0)
return v
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
val vp =container as ViewPager
val v=`object` as View
vp.removeView(v)
}
}
активность
lateinit var mPager:ViewPager
class acc0 : AppCompatActivity() {
var lang:Double?=null
var lat:Double?=null
var facebookurlintent:String?=null
lateinit var mAdView : AdView
var url:ArrayList<String>?= null
var currentPage:Int = 0
lateinit var timer :Timer
var DELAY_MS :Long= 500;//delay in milliseconds before task is to be executed
var PERIOD_MS :Long= 3000; // time in milliseconds between successive task executions.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.acc0_layout)
url?.add("url")
url?.add("url")
var intentpass=intent?.extras
var offer=intentpass?.getString("offer").toString()
var offer1=intentpass?.getString("offer1")
// val images= arrayOf(R.drawable.abstract2,R.drawable.air)
mPager=findViewById<View>(R.id.viewpager) as ViewPager
mPager!!.adapter= pagerview( this,url!!)
updatepage()
mPager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
}
override fun onPageSelected(position: Int) {
currentPage=position
}
})
fun updatepage(){
var handler= Handler()
val update:Runnable= Runnable {
if(currentPage==images.size){
currentPage=0
}
mPager.setCurrentItem(currentPage++,true)
}
timer= Timer()
timer.schedule(object :TimerTask(){
override fun run() {
handler.post(update)
}
},DELAY_MS,PERIOD_MS)
}