1.首先要在layout写一个xml文件,写关于ViewPager的一个标签的xml文件
activity_main.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
2.然后写你要滑动的view,每个view文件
view1.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"
android:orientation="vertical"
android:background="@drawable/tu1"
>
<!-- android:gravity="center_horizontal" -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view1"
android:textSize="28sp"
android:id="@+id/textview"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
view2.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"
android:orientation="vertical"
android:background="@drawable/tu2"
>
<!-- android:gravity="center_horizontal" -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view2"
android:textSize="28sp"
android:id="@+id/textview2"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
view3.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"
android:orientation="vertical"
android:background="@drawable/tu3"
>
<!-- android:gravity="center_horizontal" -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view3"
android:textSize="28sp"
android:id="@+id/textview3"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
view4.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"
android:orientation="vertical"
android:background="@drawable/tu4"
>
<!-- android:gravity="center_horizontal" -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view4"
android:textSize="28sp"
android:id="@+id/textview4"
android:layout_gravity="center_horizontal"
/>
<Button
android:onClick="enter"
android:text="点击进入主界面"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
3、然后要把四个view (view1到4)用代码加载到ViewPager里面去,前提是要生成一个PagerAdapter,
通过这个PagerAdapter把数据加载到ViewPager里面去
MainActivity.java
public class MainActivity extends Activity {
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) findViewById(R.id.viewpager);
LayoutInflater layoutInflater=getLayoutInflater().from(this);
View view1=layoutInflater.inflate(R.layout.view1, null);
View view2=layoutInflater.inflate(R.layout.view2, null);
View view3=layoutInflater.inflate(R.layout.view3, null);
View view4=layoutInflater.inflate(R.layout.view4, null);
List<View> list=new ArrayList<View>();
list.add(view1);
list.add(view2);
list.add(view3);
list.add(view4);
viewPager.setAdapter(new MyViewPagerAdapter(list));
viewPager.setCurrentItem(0);
}
public void enter(View view)
{
Intent intent = new Intent(getApplicationContext(), OtherActivity.class);
startActivity(intent);
}
@Override
protected void onStop() //重写onStop,在Activity onStop的时候Finish该Activity
{
finish();
super.onStop();
}
}
MyViewPagerAdapter.java
public class MyViewPagerAdapter extends PagerAdapter{
List<View> list;
public MyViewPagerAdapter (List<View> list){
this.list=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView(list.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(list.get(position),0);
return list.get(position);
}
}