继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

ViewPager(实现引导页功能)

EdgarX
关注TA
已关注
手记 5
粉丝 6
获赞 133
  • 首先在引导界面的布局文件定义一个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:src="@drawable/login_point" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/iv2"
            android:src="@drawable/login_point" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/iv3"
            android:src="@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();
            }
        }
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP

热门评论

感谢楼主、写得很详细

查看全部评论