ViewPager设置启动页(仅供自己学习使用)

学习与此
启动页代码

public class SplashActivity extends AppCompatActivity {

    private final int SPLASH_DISPLAY_LENGHT = 2000; // 延迟2秒
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //获得sharedpreferences的实体,第二个参数为模式
        preferences = getSharedPreferences("isFirstStart", Context.MODE_PRIVATE);
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                if (preferences.getBoolean("firststart", true)) {
                    editor = preferences.edit();
                    // 将登录标志位设置为false,下次登录时不再显示引导页面
                    editor.putBoolean("firststart", false);
                    editor.commit();
                    Intent intent = new Intent();
                    intent.setClass(SplashActivity.this, WelcomeActivity.class);
                    SplashActivity.this.startActivity(intent);
                    SplashActivity.this.finish();
                } else {
                    Intent intent = new Intent();
                    intent.setClass(SplashActivity.this, LoginActivity.class);
                    SplashActivity.this.startActivity(intent);
                    SplashActivity.this.finish();
                }
            }
        }, SPLASH_DISPLAY_LENGHT);
    }
}

AndroidManifest中设置theme背景

        
            
                
                
            

styles设置启动页背景

    

    

WelcomeActivity代码

  • 图片设置setImageResource图片不能铺满屏幕,采取setBackgroundResource方法
  • dots[i] = (ImageView) ll_dot.getChildAt(i);//学到了通过这种方法得到线性布局子布局
public class WelcomeActivity extends AppCompatActivity  implements ViewPager.OnPageChangeListener,View.OnClickListener {

    private ViewPager vp;
    private ViewPagerAdapter vpAdapter;
    private List views;
    private LinearLayout ll_dot;
    private Button btn_goLogin;
    // 引导图片资源
    private static final int[] pics = { R.drawable.guide1, R.drawable.guide2, R.drawable.guide3};

    // 底部小点图片
    private ImageView[] dots;

    // 记录当前选中位置
    private int currentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        initGui();
        initAction();
        initViewPager();
        // 初始化底部小圆点
        initDots();
    }

    private void initGui() {
        btn_goLogin = (Button) findViewById(R.id.btn_gologin);
        vp = (ViewPager) findViewById(R.id.viewpager);
        ll_dot = (LinearLayout) findViewById(R.id.ll_dot);
    }

    protected void initAction() {
        btn_goLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_gologin:
                Intent intent = new Intent();
                intent.setClass(WelcomeActivity.this, LoginActivity.class);
                WelcomeActivity.this.startActivity(intent);
                finish();
                break;
            default:
                break;
        }
    }

    private void initViewPager() {
        views = new ArrayList();
        // 为引导图片提供布局参数
     //   LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        // 初始化引导图片列表
        for (int i = 0; i < pics.length; i++) {
            ImageView iv = new ImageView(this);
            //iv.setLayoutParams(mParams);
          //  iv.setImageResource(pics[i]);这种方式不能图片铺满屏幕
           iv.setBackgroundResource(pics[i]);
            views.add(iv);
        }
        // 初始化Adapter
        vpAdapter = new ViewPagerAdapter(views);
        vp.setAdapter(vpAdapter);
        // 绑定回调
        vp.setOnPageChangeListener(this);
    }



    private void initDots() {
        dots = new ImageView[pics.length];
        // 循环取得小点图片
        for (int i = 0; i < pics.length; i++) {
            //View android.view.ViewGroup.getChildAt(int index):
            //Returns the view at the specified position in the group.
            dots[i] = (ImageView) ll_dot.getChildAt(i);//以后通过这种方法得到线性布局子布局
            dots[i].setEnabled(false);// 都设为黑色
            dots[i].setTag(i);// 设置位置tag,方便取出与当前位置对应
            //View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来。
        }

        currentIndex = 0;
        dots[currentIndex].setEnabled(true);// 设置为红色,即选中状态
    }

    /**
     * 改变当前引导小点颜色ؿ
     */
    private void setCurDot(int positon) {
        if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
            return;
        }
        System.out.println("positon="+positon);
        dots[positon].setEnabled(true);
        //此时的currentIndex指的是上一个圆点
        System.out.println("currentIndex="+currentIndex);
        dots[currentIndex].setEnabled(false);
        //现在的currentIndex指的是当前的小圆点
        currentIndex = positon;
    }

    // 当滑动状态改变时调用
    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    // 当前页面被滑动时调用
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    // 当新的页面被选中时调用
    @Override
    public void onPageSelected(int arg0) {
        // 设置底部小点选中状态
        setCurDot(arg0);
        if (arg0 == 2) {
            btn_goLogin.setVisibility(View.VISIBLE);
        } else {
            btn_goLogin.setVisibility(View.GONE);
        }
    }
}

WelcomeActivity xml文件




    


    

        

        

        


    
    

使用xml定义小圆点的选择背景




    
    
    


dot_false即为未选择图片小圆点的颜色



    
    
        
        
    
    
    
    
    
    

    
    
        
        
 

dot_true即为选择图片小圆点的颜色



    
    
        
        
    
    
    
    
    
    

    
    
        
        
 

适配器

public class ViewPagerAdapter extends PagerAdapter {
    private List mViewList ;

    public ViewPagerAdapter(List mViewList ) {
        this.mViewList = mViewList;
    }

    @Override
    public int getCount() {
        return mViewList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(mViewList.get(position));
        return mViewList.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mViewList.get(position));
    }
}

你可能感兴趣的:(viewpager)