viewpager是一个很有用的view容器,当你的应用拥有许多不同的数据页面,你需要支持左右滑屏来切换时。要使用viewpager,你必须创建一个pageAdapter用来提供Viewpager的数据。FRAGMENT通常用于将数据分页面显示。
1.定义layout
<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:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<android.support.v4.view.PagerTabStrip
android:id="@+id/tabstrip"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:gravity="center" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
2.定义四个tab子页面
tab1.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="@color/red">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/tab1"/>
</LinearLayout>
tab2.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="@color/green">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/tab2"/>
</LinearLayout>
tab3.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="@color/yellow">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/tab3"/>
</LinearLayout>
tab4.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="@color/blue">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/tab4"/>
</LinearLayout>
3.java文件
package com.dou.layoutdesign;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ViewPagerActivity extends ActionBarActivity {
ViewPager pager = null;
PagerTabStrip tabStrip = null;
ArrayList<View> viewContainter = new ArrayList<View>();
ArrayList<String> titleContainer = new ArrayList<String>();
public String TAG = "tag";
@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
pager = (ViewPager) this.findViewById(R.id.viewpager);
tabStrip = (PagerTabStrip) this.findViewById(R.id.tabstrip);
//取消tab下面的长横线
tabStrip.setDrawFullUnderline(false);
//设置tab的背景色
tabStrip.setBackgroundColor(this.getResources().getColor(R.color.color1));
//设置当前tab页签的下划线颜色
tabStrip.setTabIndicatorColor(this.getResources().getColor(R.color.color2));
tabStrip.setTextSpacing(200);
View view1 = LayoutInflater.from(this).inflate(R.layout.tab1, null);
View view2 = LayoutInflater.from(this).inflate(R.layout.tab2, null);
View view3 = LayoutInflater.from(this).inflate(R.layout.tab3, null);
View view4 = LayoutInflater.from(this).inflate(R.layout.tab4, null);
//viewpager开始添加view
viewContainter.add(view1);
viewContainter.add(view2);
viewContainter.add(view3);
viewContainter.add(view4);
//页签项
titleContainer.add("网易新闻");
titleContainer.add("网易体育");
titleContainer.add("网易财经");
titleContainer.add("网易女人");
pager.setAdapter(new PagerAdapter() {
//viewpager中的组件数量
@Override
public int getCount() {
return viewContainter.size();
}
//滑动切换的时候销毁当前的组件
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
((ViewPager) container).removeView(viewContainter.get(position));
}
//每次滑动的时候生成的组件
@Override
public Object instantiateItem(ViewGroup container, int position) {
((ViewPager) container).addView(viewContainter.get(position));
return viewContainter.get(position);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
@Override
public CharSequence getPageTitle(int position) {
return titleContainer.get(position);
}
});
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
Log.d(TAG, "--------changed:" + arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
Log.d(TAG, "-------scrolled arg0:" + arg0);
Log.d(TAG, "-------scrolled arg1:" + arg1);
Log.d(TAG, "-------scrolled arg2:" + arg2);
}
@Override
public void onPageSelected(int arg0) {
Log.d(TAG, "------selected:" + arg0);
}
});
}
}