对页面滑动感兴趣的初学者来说,viewpager无疑是很具诱惑力的一个工具类。
首先在布局文件中添加viewpager,然后在activity中对viewpager的adapter进行重写来完成最简单的viewpager功能。
具体实现的代码如下:
view_page_adapter_test.xml文件,是activity的布局文件,在其中创建viewpager
<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"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="@+id/ViewPager01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.PagerTabStrip
android:id="@+id/pagerTabStrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
</android.support.v4.view.ViewPager>
</LinearLayout>
同时要创建滑动切换的三个页面的布局文件,如下:
view_page_adapter_test01.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=".ViewPageAdapterTest" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-------测试页面2-----" />
</RelativeLayout>
view_page_adapter_test02.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=".ViewPageAdapterTest" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-------测试页面2-----" />
</RelativeLayout>
view_page_adapter_test03.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=".ViewPageAdapterTest" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-------测试页面3-----" />
</RelativeLayout>
ViewPageAdapterTest.java文件(创建的activity)
package com.example.ctroltest.viewpager;
import java.util.ArrayList;
import java.util.List;
import com.example.ctroltest.R;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
public class ViewPageAdapterTest extends Activity {
private ViewPager VP01;
private View view01,view02,view03;
private List<View> viewList=new ArrayList<View>(); //定义用来显示页面的
private List<String> titleList=new ArrayList<String>(); //定义显示页面的标题
private PagerTitleStrip pagerTitleStrip;//定义viewpager的标题
private PagerTabStrip pagerTabStrip;//一个viewpager的指示器。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_page_adapter_test);
VP01 = (ViewPager) findViewById(R.id.ViewPager01);
myViewPagerAdapter pagerAdapter01=new myViewPagerAdapter();
LayoutInflater lf = getLayoutInflater().from(this);
//加载viewpage滑动展现的页面
view01 = lf.inflate(R.layout.view_page_adapter_test01, null);
view02 = lf.inflate(R.layout.view_page_adapter_test02, null);
view03= lf.inflate(R.layout.view_page_adapter_test03, null);
viewList.add(view01);
viewList.add(view02);
viewList.add(view03);
//添加标题名称
titleList.add("标题01");
titleList.add("标题02");
titleList.add("标题03");
pagerTabStrip=(PagerTabStrip)findViewById(R.id.pagerTabStrip);
pagerTabStrip.setTabIndicatorColor(Color.RED);
pagerTabStrip.setDrawFullUnderline(false);
pagerTabStrip.setBackgroundColor(Color.GRAY);
pagerTabStrip.setTextSpacing(50);
VP01.setAdapter(pagerAdapter01);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_page_adapter_test, menu);
return true;
}
/*
* When you implement a PagerAdapter, you must override the following
* methods at minimum:
*
* instantiateItem(ViewGroup, int) destroyItem(ViewGroup, int, Object)
* getCount() isViewFromObject(View, Object)
*/
//创建一个PagerAdapter,复写其中的方法
public class myViewPagerAdapter extends PagerAdapter {
@Override
//定义适配器初始化view时对应的 view
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
//指定当前页面的view,返回 当前view对象
container.addView(viewList.get(position));
return viewList.get(position);
//return super.instantiateItem(container, position);
}
@Override
//定义翻页操作室要销毁的veiw的方法
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
//super.destroyItem(container, position, object);
container.removeView(viewList.get(position));
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//返回ViewPager的页面数量
return viewList.size();
}
@Override
/*Determines whether a page View is associated with a specific key object as returned by instantiateItem(ViewGroup, int). This method is required for a PagerAdapter to function properly.
*/
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
//显示标题时复写的方法
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return titleList.get(position);
}
}
}
具体效果为:
这样一个最简单的viewpager实现完成了。是不是很方便呢!