不多说,直接贴代码
val dialog = Dialog(context!!)
dialog.setContentView(R.layout.dialog_imageview)
dialog.window!!.setBackgroundDrawableResource(android.R.color.black)
val layoutParams = dialog.window!!.attributes
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
dialog.window!!.attributes = layoutParams
dialog.vp.adapter = object : PagerAdapter() {
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view == `object`
}
override fun getCount(): Int = data.size
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val view = LayoutInflater.from(context).inflate(R.layout.item_dialog_image, dialog.fl_container, false)
context?.let { Glide.with(it).load(data[position]).into(view.iv_ss) }
container.addView(view)
return view
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as View)
}
}
dialog.vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
dialog.tv_position.text = "${position + 1}/${data.size}"
}
override fun onPageSelected(position: Int) {
}
})
dialog.tv_position.text = "${position + 1}/${data.size}"
dialog.iv_close.setOnClickListener { dialog.cancel() }
dialog.vp.currentItem = position
dialog.show()
这里setContentView中的参数为Dialog的自定义布局,然后还因为Dialog的宽度无法填满手机屏幕,所以使用获取window然后把宽度改为MATCH_PARENT,整体思路就是Dialog嵌套一个ViewPager