ViewPager(实现引导页功能)

  • 首先在引导界面的布局文件定义一个ViewPager:

      
    
  • 再定义几个ImageView来显示不同的图片:
    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">

      
    
      
    
      
    
      
    
  • 设置ViewPager的Adapter,继承自PagerAdapter:

      public class MyAdapter extends PagerAdapter {
          private List views;
          private Context context;
          public MyAdapter(List 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();
          }
      }

你可能感兴趣的:(ViewPager(实现引导页功能))