自己实现了一个简单的ViewPager实例,分享一下。
我的大概思路是下面图示过程:
代码链接下载地址:http://download.csdn.net/detail/lxqljc/7663053
1.代码结构如下:
2.MainActivity.java代码
package com.example.viewpagerdemo2;
import java.util.ArrayList;
import com.example.adapter.MyPagerAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
public class MainActivity extends Activity {
//1.创建一个viewpager容器对象
private ViewPager viewpager;
private ArrayList<View> pages;
private MyPagerAdapter myPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.找到需要的容器对象
initView();
//2.获得要装载的数据页面
getPages();
//3.将集合页面装载到MyPagerAapter适配器中,定义在另外一个单独的文件中,需要传入当前上下文对象和页面数据集合,并且将适配器绑定在
setAdapter();
}
/**
* 初始化组件
*/
public void initView()
{
viewpager = (ViewPager) findViewById(R.id.viewpager);
}
/**
* 获得数据源
*/
public void getPages()
{
//这里自定义三个页面
pages = new ArrayList<View>();
//将布局文件转换为对象
View page1 =LayoutInflater.from(this).inflate(R.layout.page1, null);
View page2 =LayoutInflater.from(this).inflate(R.layout.page2, null);
View page3 =LayoutInflater.from(this).inflate(R.layout.page3, null);
//将所有页面对象添加到集合对象中
pages.add(page1);
pages.add(page2);
pages.add(page3);
}
/**
* 设置适配器
*/
public void setAdapter()
{
myPagerAdapter= new MyPagerAdapter(this, pages);
//将适配器对象绑定到viewpager容器中
viewpager.setAdapter(myPagerAdapter);
}
}
3.MyPagerAdapter.java代码文件
package com.example.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class MyPagerAdapter extends PagerAdapter{
private Context context;
private ArrayList<View> pages;
public MyPagerAdapter(Context context, ArrayList<View> pages) {
super();
this.context = context;
this.pages = pages;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pages.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
//将要显示的页面添加到container容器中
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(pages.get(position));
return pages.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView(pages.get(position));
}
}
4.布局文件部分
①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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</android.support.v4.view.ViewPager>
</RelativeLayout>
②page1.xml(page1,page2,page3都是一样的布局,只是改了文字和背景而已,此处就贴其中一个文件)
<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:background="#f00"
android:gravity="center"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第一个界面"
>
</TextView>
</RelativeLayout>
ok。。。