Sometimes as long as the simple implementation of the boot page, online individual options and too many, here with a very simple viewpager to achieve, and through SharedPreferences to save the first state, the subsequent opening of the App will not be displayed
class WelcomeActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?). {
super.onCreate(savedInstanceState)
setContentView(R.layout.welcome)
initView()
}
private fun initView(a) {
var handler = Handler()
if(! CacheUtils.getBoolean(FIRST_LOGIN)) { handler.postDelayed(Runnable {val intent = Intent()
intent.setClass(this@WelcomeActivity, MainActivity::class.java)
startActivity(intent)
finish()
}, 1000)}else {
handler.postDelayed(Runnable {
val intent = Intent()
intent.setClass(this@WelcomeActivity, GuidePageActivity::class.java)
startActivity(intent)
finish()
}, 1000)}}Copy the code
Create WelcomeActivity as the welcome page. CacheUtils is a SharedPreferences utility class that performs a one-second task with a handler to ensure that the welcome page does not change too quickly
class GuidePageActivity : BaseActivity() {
private lateinit var adapter: GuidePageViewPagerAdapter
/** * hold the image array */
private val mPageList = listOf<Int>(
R.mipmap.ic_page1,
R.mipmap.ic_page2,
R.mipmap.ic_page3
)
override fun onCreate(savedInstanceState: Bundle?). {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_guidepage)
initView()
}
private fun initView(a) {
adapter = GuidePageViewPagerAdapter(mPageList, this)
viewPager.adapter = adapter
}
}
Copy the code
GuidePageActivity guides the interface, passes an array of images to the Adapter, and viewPager loads the adapter
Activity_guidepage.xml Viewpager layout file
<? xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.viewpager.widget.ViewPager>
</LinearLayout>
Copy the code
ViewpagerApadper inherits the pagerAdapter implementation of the necessary methods
class GuidePageViewPagerAdapter(var Images: List<Int>, var activity: AppCompatActivity) :
PagerAdapter() { override fun instantiateItem(container: ViewGroup, position: Int): Any { val url = Images!! [position] var view = LayoutInflater.from(activity).inflate(R.layout.item_guidepage, null) as RelativeLayout Glide.with(activity).load(Images[position]).into(view.image) container.addView(view) View.setonclicklistener {// Determine the click event on the last pageif (position == 2) {
CacheUtils.putBoolean(AppConstants.FIRST_LOGIN, false)
val intent = Intent()
intent.setClass(activity, MainActivity::class.java)
activity.startActivity(intent)
activity.finish()
}
}
return view
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as View)
}
override fun isViewFromObject(view: View, obj: Any): Boolean {
return view == obj
}
override fun getCount(): Int {
return Images.size
}
}
Copy the code
item_guidepage.xml
<? xml version="1.0" encoding="utf-8"? > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"></ImageView>
</RelativeLayout>
Copy the code
In the instantiateItem method, add a resource layout to the ViewGroup. You can customize the layout by adding buttons and text, or simply replacing it with an ImageView if you’re just displaying images. For my part, it is very simple, adding a click event to the last guide layout, so that the user clicks directly to the home page. More rich operations such as adding small dots, the easiest way. Let the UI add dots to the guide. Or you can add dots on the fly and follow the viewPager page as it changes