ViewPager 的父类是ViewGroup,是一个容器,可以做出页面左右滑动效果!
我们先看布局文件
<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"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
这里需要注意的是ViewPager在v4包里。
下面看下Activity类,注释很详细,往内部类传递数据时候使用了构造方法
package com.example.android_viewpager_1;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
// ViewPager 的父类是ViewGroup,是一个容器,可以做出页面左右滑动效果!
private ViewPager vp;
private List<View> views;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
// ViewPager与ListView类似,也需要适配器(适配器的作用:1,确定每一条目/界面的样子;2,绑定数据;3,确定个数)。
View vp_one =View.inflate(this, R.layout.vp_one, null);
View vp_two = View.inflate(this, R.layout.vp_two, null);
View vp_three = View.inflate(this, R.layout.vp_three, null);
views = new ArrayList<View>();
views.add(vp_one);
views.add(vp_two);
views.add(vp_three);
vp.setAdapter(new MyAdapter(this,views));// 确定每一个滑动页面的样式
}
private class MyAdapter extends PagerAdapter {
private Context context;
private List<View> views;
// 通过构造方法,把views 传递进来
public MyAdapter(Context context, List<View> views) {
this.views = views;
}
// 确定个数,联想ListView中getItemCount();
@Override
public int getCount() {
return views.size();
}
// 传进来的必须是一个View否则无法展示出来
@Override
public boolean isViewFromObject(View view, Object object) {
return object == view;
}
/**
* 初始化每一个页面,绑定view到ViewPager上 联想listview中的getView()方法!
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
// 找到position位置的view对象
View view = views.get(position);
// 将我们找到的view对象添加到viewpager中
container.addView(view);
System.out.println("初始化"+position);
return view;
}
// view被花出去后,删掉View
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// 我们自己删,不调用父类的
// super.destroyItem(container, position, object);
// 找到position位置的view
View view = views.get(position);
// 将找到的View对象从Viewpager中移除!
container.removeView(view);
System.out.println("销毁"+position);
}
}
}
Activity类里需要三个布局文件;
第一个布局文件
<?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:background="#123456"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一个页面"/>
</LinearLayout>
第二个布局文件
<?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:background="#f23456"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第二个页面"/>
</LinearLayout>
第三个布局文件
<?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:background="#a23456"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第三个页面"/>
</LinearLayout>
但这样写也有一个不好的地方,根据日志打印可以发现,频繁的创建和销毁对象!问题的解决我们在下一博客。