标签:
一、页面布局文件
welcome.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/viewpager_welcome" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" > </android.support.v4.view.ViewPager> </RelativeLayout>
welcome_one.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/bg_icon" android:gravity="center" android:text="one" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>
welcome_two.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/bg_icon" android:gravity="center" android:text="two" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>
welcom_three.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_welcomethree" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/bg_icon" android:gravity="center" android:text="three" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/btn_justtogo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录"/> </LinearLayout>
WelcomeActivity.java
package com.mytest.huilife; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.view.View; import android.widget.Button; import android.widget.Toast; public class WelcomeActivity extends Activity { private List<View> welPageList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome); View welOne = getLayoutInflater().inflate(R.layout.welcome_one, null); View welTwo = getLayoutInflater().inflate(R.layout.welcome_two, null); View welThree = getLayoutInflater().inflate(R.layout.welcome_three, null); List<View> pageList = new ArrayList<View>(); pageList.add(welOne); pageList.add(welTwo); pageList.add(welThree); ViewPager viewPager = (ViewPager) this.findViewById(R.id.viewpager_welcome); viewPager.setAdapter(new WelcomePagerAdapter(pageList)); Button btnJusttogo = (Button)welThree.findViewById(R.id.btn_justtogo); btnJusttogo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(WelcomeActivity.this, "just to go ", Toast.LENGTH_SHORT).show(); } }); } }
WelcomePagerAdapter.java
package com.mytest.huilife; import java.util.List; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; public class WelcomePagerAdapter extends PagerAdapter { private List<View> pageList; public WelcomePagerAdapter(List<View> pageList) { this.pageList = pageList; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ViewGroup vg = (ViewGroup) arg0; vg.removeView(pageList.get(arg1)); } @Override public void finishUpdate(View arg0) { } @Override public int getCount() { return pageList.size(); } @Override public Object instantiateItem(View arg0, int arg1) { ViewGroup vg = (ViewGroup) arg0; vg.addView(pageList.get(arg1)); return pageList.get(arg1); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View arg0) { } }
标签:
原文地址:http://www.cnblogs.com/2015android/p/4676036.html