ViewPager implements the boot page (add a navigation point to determine whether to enter the main page for the first time), the main page of viewpager
1. Four page la s on the boot page, containing a background image
Insert it to the guide interface layout (fragment is not used here)
Guide_background_fragment1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" ><ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/guide_background1"/></LinearLayout>
Guide_background_fragment2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" ><ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/guide_background2"/></LinearLayout>
Guide_background_fragment3.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" ><ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/guide_background3"/></LinearLayout>
Guide_background_fragment4.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" ><ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/guide_background4"/></LinearLayout>
Guide. xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <LinearLayout android:id="@+id/l1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_centerInParent="true" android:layout_alignParentBottom="true" > <ImageView android:id="@+id/imageV1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/login_point" /> <ImageView android:id="@+id/imageV2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/login_point" /> <ImageView android:id="@+id/imageV3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/login_point" /> <ImageView android:id="@+id/imageV4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/login_point" /> </LinearLayout></RelativeLayout>
2. Adapter
GuidePagerAdapter. java
import android.content.Context;import android.support.v4.view.PagerAdapter;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import java.util.List;public class GuidePagerAdapter extends PagerAdapter{ private Context context; private List<View> views; public GuidePagerAdapter(List<View> views, Context context) { this.context = context; this.views = views; } @Override public int getCount() { // TODO Auto-generated method stub return views.size(); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(views.get(position)); return views.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(views.get(position)); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return (arg0 == arg1); }}
Guide. java
Import android. app. activity; import android. content. intent; import android. OS. bundle; import android. support. v4.view. viewPager; import android. view. layoutInflater; import android. view. view; import android. view. window; import android. widget. button; import android. widget. imageView; import java. util. arrayList; import java. util. list;/*** Created by zps on 2015/9/3. */public class Guide extends Activity implements ViewPager. onPageChangeListener {private List <View> listView; private ViewPager viewPager; private GuidePagerAdapter vpAdapter; private int [] dotsId = {R. id. imageV1, R. id. imageV2, R. id. imageV3, R. id. imageV4}; private ImageView [] dotsView; private Button lijitiyanButton; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. view_pager); init (); initDots ();} private void initDots () {dotsView = new ImageView [listView. size ()]; for (int I = 0; I <listView. size (); I ++) {dotsView [I] = (ImageView) findViewById (dotsId [I]) ;}} private void init () {LayoutInflater inflater = LayoutInflater. from (this); listView = new ArrayList <View> (); // obtain the View object View view1 = inflater. inflate (R. layout. view_pager_img1, null); View view2 = inflater. inflate (R. layout. view_pager_img2, null); View view3 = inflater. inflate (R. layout. view_pager_img3, null); View view4 = inflater. inflate (R. layout. view_pager_img4, null); // place the view object in List <view> listView. add (view1); listView. add (view2); listView. add (view3); listView. add (view4); // List <view> In the adapter ViewPagerAdapter, vpAdapter = new GuidePagerAdapter (listView, this); // obtain the ViewPage and set the adapter; viewPager = (ViewPager) findViewById (R. id. viewpager); viewPager. setAdapter (vpAdapter); viewPager. addOnPageChangeListener (this); lijitiyanButton = (Button) view4.findViewById (R. id. lijitiyan_button); lijitiyanButton. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (Guide. this, MainActivity. class); startActivity (intent); finish () ;}}) ;}@ Override public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {}@ Override public void onPageSelected (int position) {for (int I = 0; I <dotsId. length; I ++) {if (I = position) {dotsView [I]. setImageResource (R. drawable. login_point_selected);} else {dotsView [I]. setImageResource (R. drawable. login_point) ;}}@ Override public void onPageScrollStateChanged (int state ){}}
3. Determine whether to enter the boot page for the first time
Public class GuideWelcome extends Activity {private static final int TIME = 2000; private static boolean isFirstIn = false; private static final int First = 1000; private static final int NoFirst = 1001; private Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {switch (msg. what) {case First: FirstIn (); break; case NoFirst: NoFirstIn (); break; default: break ;}}}; Protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. welcome); init ();} private void init () {// storage file name: "gudiewelcome" SharedPreferences sharedPreferences = getSharedPreferences ("guidewelcome", MODE_PRIVATE); isFirstIn = sharedPreferences. getBoolean ("isFirstIn", true); if (! IsFirstIn) {handler. sendEmptyMessageDelayed (NoFirst, TIME);} else {handler. sendEmptyMessageDelayed (First, TIME); // The key of the stored file is isFirstIn and the value is false SharedPreferences. editor editor = sharedPreferences. edit (); editor. putBoolean ("isFirstIn", false); editor. commit () ;}} private void FirstIn () {Intent intent = new Intent (GuideWelcome. this, Guide. class); startActivity (intent); finish ();} private void NoFirstIn () {Intent intent = new Intent (GuideWelcome. this, TabMainActivity. class); startActivity (intent); finish ();}}