基於viewpager的啓動頁界面代碼實現

class GuideActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_guide_kt)
    viewPager.adapter = GuideAdapter(getViewDatas())
}

class GuideAdapter(val list: List<View>) : PagerAdapter() {
    override fun getCount(): Int {
        return list.size
    }

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

    override fun instantiateItem(container: ViewGroup?, position: Int): Any {
        container!!.addView(list.get(position))
        return list.get(position)
    }

    override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
        container!!.removeView(list.get(position))
    }
}

/**
 * viewpager最後一頁
 */
fun getLastView(): View {
    val rootView = View.inflate(this@GuideActivity, R.layout.activity_guide_last_img_kt, null)
    Glide.with(this@GuideActivity).load(R.drawable.guide_3).into(rootView.find(R.id.last_img))
    rootView.find<ImageButton>(R.id.start_imgBtn).setOnClickListener {
      //基於業務的提示彈框
      alert {
            include<View>(R.layout.dialog_permission_hint) {
                setBackgroundResource(android.R.color.transparent)
                find<View>(R.id.btn_next).setOnClickListener {
                    finish()
                    startActivity(Intent(this@GuideActivity, StartPageActivity::class.java))
                }
            }
        }.show()
    }
    return rootView
}

fun getViewDatas(): List<View> {
    var img1 = ImageView(this)
    Glide.with(this@GuideActivity).load(R.drawable.guide_1).into(img1)
    var img2 = ImageView(this)
    Glide.with(this@GuideActivity).load(R.drawable.guide_2).into(img2)
    return listOf<View>(img1, img2, getLastView())
}
}
佈局文件activity_guide_kt
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_guide_kt"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:scrollbars="none"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>
相關文章
相關標籤/搜索