为了账号安全,请及时绑定邮箱和手机立即绑定

ViewPager(实现引导页功能)

标签:
Java Android
  • 首先在引导界面的布局文件定义一个ViewPager:
        <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vp"
        android:persistentDrawingCache="animation"/>
  • 再定义几个ImageView来显示不同的图片:
             <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/ll"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:gravity="center_horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/iv1"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/login_point" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/iv2"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/login_point" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/iv3"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/login_point" />
        </LinearLayout>
  • 设置ViewPager的Adapter,继承自PagerAdapter:

        public class MyAdapter extends PagerAdapter {
            private List<View> views;
            private Context context;
            public MyAdapter(List<View> views,Context context){
                this.views=views;
                this.context=context;
            }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }
    
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);
        }
    
        @Override
        public int getCount() {
            return views.size();
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view==object);
            }
        }
  • 最后在onCreate方法中设置Adapter:
        adapter=new MyAdapter(views,this);
        vp= (ViewPager) findViewById(R.id.vp);
        vp.setAdapter(adapter);
  • 在每个图片下面添加导航点,这就需要调用ViewPager的setOnPageChangeListener(this)方法了。并覆盖onPageSelected(int position)。
        @Override
        public void onPageSelected(int position) {
            for (int i = 0; i < ids.length; i++) {
            //被选中的页面的导航点颜色要更深一些
                if (position == i) {
                    dots[i].setImageResource(R.drawable.login_point_selected);
                } else {
                    dots[i].setImageResource(R.drawable.login_point);
                }
            }
        }
  • 第一次启动应用是启动欢迎界面,在启动引导界面,最后启动主界面,第二次后就不启动引导界面了,通过Handler来发送消息。

        public static final int time=2000;
        public static final int GOHMOE =1000;
        public static final int GOGUILD =1001;
    
        private boolean isFistIn=false;
    
        private Handler mHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case GOHMOE:
                        goHome();
                        break;
                    case GOGUILD:
                        goGuild();
                        break;
                }
            }
        };
    
        private void goHome(){
        Intent intent=new Intent(WelcomAty.this,MainActivity.class);
        startActivity(intent);
        finish();
        }private void goGuild(){
        Intent intent=new Intent(WelcomAty.this,Guild.class);
        startActivity(intent);
        finish();
        }
    
        private void init(){
        SharedPreferences preferences= getSharedPreferences("jike",MODE_PRIVATE);
        isFistIn=preferences.getBoolean("isFistIn",true);
        if (!isFistIn){
            mHandler.sendEmptyMessageDelayed(GOHMOE,time);
        }else {
    
        mHandler.sendEmptyMessageDelayed(GOGUILD,time);
            SharedPreferences.Editor editor=preferences.edit();
            editor.putBoolean("isFistIn",false);
            editor.commit();
            }
        }
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消