欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android使用ViewPager实现启动引导页效果

程序员文章站 2022-04-12 13:19:55
本文实例为大家分享了android实现启动引导页效果的具体代码,供大家参考,具体内容如下viewpagertwoactivity.javapackage cn.edu.zufe.app001;impo...

本文实例为大家分享了android实现启动引导页效果的具体代码,供大家参考,具体内容如下

Android使用ViewPager实现启动引导页效果

viewpagertwoactivity.java

package cn.edu.zufe.app001;

import androidx.annotation.nonnull;
import androidx.appcompat.app.appcompatactivity;
import androidx.viewpager.widget.pageradapter;
import androidx.viewpager.widget.viewpager;

import android.content.context;
import android.os.bundle;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.button;
import android.widget.imageview;
import android.widget.radiobutton;
import android.widget.radiogroup;
import android.widget.toast;

import java.util.arraylist;

public class viewpagertwoactivity extends appcompatactivity {

    private int[] launchimagearray = new int[]{
            r.drawable.guide_bg1,r.drawable.guide_bg2,r.drawable.guide_bg3,r.drawable.guide_bg4
    };
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_view_pager_two);
        viewpager vp_launch = (viewpager)findviewbyid(r.id.vp_launch);
        launchsimpleadapter adapter = new launchsimpleadapter(this,launchimagearray);
        vp_launch.setadapter(adapter);
        vp_launch.setcurrentitem(0);
    }

    class launchsimpleadapter extends pageradapter{
        private context mcontext;
        private arraylist<view> mviewlist = new arraylist<view>();
        public launchsimpleadapter(final context context, int[] imagearray){
            mcontext = context;
            for(int i=0; i<imagearray.length; i++){
                view view = layoutinflater.from(context).inflate(r.layout.item_launch,null);
                imageview iv_launcher = (imageview)view.findviewbyid(r.id.iv_launch);
                radiogroup rg_indicate = (radiogroup)view.findviewbyid(r.id.rg_indicate);
                button btn_start = (button)view.findviewbyid(r.id.btn_start);
                iv_launcher.setimageresource(imagearray[i]);
                for(int j=0; j<imagearray.length; j++){
                    radiobutton radio = new radiobutton(mcontext);
                    radio.setlayoutparams(new viewgroup.layoutparams(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content));
                    radio.setbuttondrawable(r.drawable.launch_guide);
                    radio.setpadding(10,10,10,10);
                    rg_indicate.addview(radio);
                }
                ((radiobutton)rg_indicate.getchildat(i)).setchecked(true);
                if (i == imagearray.length-1) {
                    btn_start.setvisibility(view.visible);
                    btn_start.setonclicklistener(new view.onclicklistener() {
                        @override
                        public void onclick(view v) {
                            toast.maketext(context,"欢迎您开启美好生活",toast.length_short).show();
                        }
                    });
                }
                mviewlist.add(view);
            }
        }

        @override
        public void destroyitem(@nonnull viewgroup container, int position, @nonnull object object) {
            container.removeview(mviewlist.get(position));
        }

        @nonnull
        @override
        public object instantiateitem(@nonnull viewgroup container, int position) {
            container.addview(mviewlist.get(position));
            return mviewlist.get(position);
        }

        @override
        public int getcount() {
            return mviewlist.size();
        }

        @override
        public boolean isviewfromobject(@nonnull view view, @nonnull object o) {
            return view == o;
        }
    }
}

activity_view_pager_two.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cn.edu.zufe.app001.viewpagertwoactivity">

    <androidx.viewpager.widget.viewpager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vp_launch">
    </androidx.viewpager.widget.viewpager>
</androidx.constraintlayout.widget.constraintlayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。