应用引导页是现今App的必需品,通过引导页可以第一时间知道此款软件的特色,如何使用。并且能了解到最新的动态和更新。
第一页,Logo页面或者欢迎页面,判断是否需要进入引导页。
1 package com.zhang.viewpagerdemo; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.content.SharedPreferences; 6 import android.os.Bundle; 7 import android.os.Handler; 8 import android.os.Message; 9 10 public class WelcomeAct extends Activity { 11 12 private boolean isFirstIn = false; 13 private static final int TIME = 2000; 14 private static final int GO_HOME = 1000; 15 private static final int GO_GUIDE = 1001; 16 17 private Handler handler = new Handler() { 18 19 @Override 20 public void handleMessage(Message msg) { 21 super.handleMessage(msg); 22 23 switch (msg.what) { 24 case GO_HOME: 25 goHome(); 26 break; 27 case GO_GUIDE: 28 goGuide(); 29 break; 30 } 31 } 32 }; 33 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.welcome); 38 39 init(); 40 } 41 42 private void init() { 43 SharedPreferences preferences = getSharedPreferences("myWelcome", MODE_PRIVATE); 44 isFirstIn = preferences.getBoolean("isFirstIn", true); 45 if (!isFirstIn) { 46 handler.sendEmptyMessageDelayed(GO_HOME, TIME); 47 } else { 48 handler.sendEmptyMessageDelayed(GO_GUIDE, TIME); 49 SharedPreferences.Editor editor = preferences.edit(); 50 editor.putBoolean("isFirstIn", false); 51 editor.apply(); 52 } 53 } 54 55 private void goHome() { 56 Intent i = new Intent(WelcomeAct.this, MainActivity.class); 57 startActivity(i); 58 finish(); 59 } 60 61 private void goGuide() { 62 Intent i = new Intent(WelcomeAct.this, Guide.class); 63 startActivity(i); 64 finish(); 65 } 66 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context="com.zhang.viewpagerdemo.WelcomeAct"> 8 9 <ImageView 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:background="@drawable/welcome_android" /> 13 14 </LinearLayout>
引导页面:
1 package com.zhang.viewpagerdemo; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.v4.view.ViewPager; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.widget.Button; 10 import android.widget.ImageView; 11 12 import java.util.ArrayList; 13 import java.util.List; 14 15 /** 16 * Created by Mr.Z on 2016/5/22 0022. 17 */ 18 public class Guide extends Activity implements ViewPager.OnPageChangeListener { 19 20 private ViewPager vp; 21 private ViewPagerAdapter vpAdapter; 22 private List<View> views; 23 private ImageView[] dots; 24 private int[] ids = {R.id.iv1, R.id.iv2, R.id.iv3}; 25 private Button btnStart; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.guide); 31 32 initViews(); 33 34 initDots(); 35 } 36 37 private void initViews() { 38 39 LayoutInflater inflater = LayoutInflater.from(this); 40 41 views = new ArrayList<>(); 42 views.add(inflater.inflate(R.layout.one, null)); 43 views.add(inflater.inflate(R.layout.two, null)); 44 views.add(inflater.inflate(R.layout.three, null)); 45 46 vpAdapter = new ViewPagerAdapter(views, this); 47 48 btnStart = (Button) views.get(2).findViewById(R.id.start_btn); 49 50 vp = (ViewPager) findViewById(R.id.viewpager); 51 vp.setAdapter(vpAdapter); 52 vp.setOnPageChangeListener(this); 53 54 btnStart.setOnClickListener(new View.OnClickListener() { 55 @Override 56 public void onClick(View v) { 57 Intent i = new Intent(Guide.this,MainActivity.class); 58 startActivity(i); 59 finish(); 60 } 61 }); 62 63 } 64 65 private void initDots() { 66 dots = new ImageView[views.size()]; 67 for (int i = 0; i < views.size(); i++) { 68 dots[i] = (ImageView) findViewById(ids[i]); 69 } 70 } 71 72 @Override 73 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 74 75 } 76 77 @Override 78 public void onPageSelected(int position) { 79 80 } 81 82 @Override 83 public void onPageScrollStateChanged(int state) { 84 for (int i = 0; i < ids.length; i++) { 85 if (state == i) { 86 dots[i].setImageResource(R.drawable.login_point_selected); 87 } else { 88 dots[i].setImageResource(R.drawable.login_point); 89 } 90 } 91 } 92 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <android.support.v4.view.ViewPager 7 android:id="@+id/viewpager" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:background="@android:color/black" 11 android:flipInterval="30" 12 android:persistentDrawingCache="animation" /> 13 14 <LinearLayout 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:layout_alignParentBottom="true" 18 android:gravity="center_horizontal" 19 android:orientation="horizontal"> 20 21 <ImageView 22 android:id="@+id/iv1" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:src="@drawable/login_point" /> 26 <ImageView 27 android:id="@+id/iv2" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:src="@drawable/login_point" /> 31 <ImageView 32 android:id="@+id/iv3" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:src="@drawable/login_point" /> 36 </LinearLayout> 37 38 </RelativeLayout>
1 package com.zhang.viewpagerdemo; 2 3 import android.content.Context; 4 import android.support.v4.view.PagerAdapter; 5 import android.support.v4.view.ViewPager; 6 import android.view.View; 7 8 import java.util.List; 9 10 /** 11 * Created by Mr.Z on 2016/5/22 0022. 12 */ 13 public class ViewPagerAdapter extends PagerAdapter { 14 15 private List<View> views; 16 private Context context; 17 18 public ViewPagerAdapter(List<View> views, Context context) { 19 this.views = views; 20 this.context = context; 21 } 22 23 @Override 24 public void destroyItem(View container, int position, Object object) { 25 ((ViewPager) container).removeView(views.get(position)); 26 } 27 28 @Override 29 public Object instantiateItem(View container, int position) { 30 31 ((ViewPager) container).addView(views.get(position)); 32 33 return views.get(position); 34 } 35 36 @Override 37 public int getCount() { 38 return views.size(); 39 } 40 41 @Override 42 public boolean isViewFromObject(View view, Object object) { 43 return (view == object); 44 } 45 }
主页面:
1 package com.zhang.viewpagerdemo; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 6 public class MainActivity extends AppCompatActivity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 } 13 }
时间: 2024-10-29 08:22:34