ViewPager的基础用法,记录一下
1.新建Android项目ViewPager
2.主布局文件activity_main.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.support.v4.view.ViewPager
android:id="@+id/vp_pagers"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
3.新建多个页放入ViewPager中,pager01.xml,pager02.xml,pager03.xml代码如下
pager01.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" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0050ff"
android:gravity="center"
android:text="Page 01"
android:textColor="#ffffff"
android:textSize="36sp" />
</LinearLayout>
pager02.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" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#339933"
android:gravity="center"
android:text="Page 02"
android:textColor="#ffffff"
android:textSize="36sp" />
</LinearLayout>
pager03.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" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AA3300"
android:gravity="center"
android:text="Page 03"
android:textColor="#ffffff"
android:textSize="36sp" />
</LinearLayout>
4.MainAcitivity.java文件中的代码
package cn.ywzj.viewpager;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
/**
* ViewPager
*/
private ViewPager viewPagers;
/**
* PagerAdapter
*/
private PagerAdapter adapter;
/**
* 在ViewPager中需要显示的View对象(集合)
*/
private List<View> lists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
viewPagers = (ViewPager) findViewById(R.id.vp_pagers);
// 准备数据--在ViewPager中要显示的View对象
lists = new ArrayList<View>();
lists.add(getLayoutInflater().inflate(R.layout.pager01, null));
lists.add(getLayoutInflater().inflate(R.layout.pager02, null));
lists.add(getLayoutInflater().inflate(R.layout.pager03, null));
// 创建PagerAdapter
adapter=new InnerViewPagerAdapter();
// 为ViewPager配置Adapter,以显示内容
viewPagers.setAdapter(adapter);
}
public class InnerViewPagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
// 根据参数position确定需要显示的View对象
View view = lists.get(position);
// 将需要显示的View对象添加到参数container容器中
container.addView(view);
// 返回需要显示的View对象
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// 以下调用父类的同名方法的代码必须删除!!!
// super.destroyItem(container, position, object);
// 把第position个显示的View对象从容器container中移除
container.removeView(lists.get(position));
}
@Override
public int getCount() {
return lists.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
效果图如下: