viewpager使用

ViewPager用于实现多页面的切换效果,该类存在于Google的兼容包里面,所以在引用时记得在BuilldPath中加入“android-support-v4.jar”

主布局文件

main.xml

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:layout_width="fill_parent"
04     android:layout_height="fill_parent"
05     android:orientation="vertical" >
06  
07     <android.support.v4.view.ViewPager
08         android:id="@+id/viewpager"
09         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_gravity="center" >
12  
13         <android.support.v4.view.PagerTitleStrip
14             android:id="@+id/pagertitle"
15             android:layout_width="wrap_content"
16             android:layout_height="wrap_content"
17             android:layout_gravity="top" />
18     </android.support.v4.view.ViewPager>
19  
20 </LinearLayout>
其中ViewPager为多页显示控件,PagerTitleStrip用于显示当前页面的标题

主窗口代码:

PagerTitleDemoActivity.java

01 package com.ns.pager;
02  
03 import java.util.ArrayList;
04  
05 import android.app.Activity;
06 import android.os.Bundle;
07 import android.support.v4.view.PagerAdapter;
08 import android.support.v4.view.PagerTitleStrip;
09 import android.support.v4.view.ViewPager;
10 import android.view.LayoutInflater;
11 import android.view.View;
12  
13 public class PagerTitleDemoActivity extends Activity {
14     /** Called when the activity is first created. */
15     private ViewPager mViewPager;
16     private PagerTitleStrip mPagerTitleStrip;
17     @Override
18     public void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.main);
21         mViewPager = (ViewPager)findViewById(R.id.viewpager);
22         mPagerTitleStrip = (PagerTitleStrip)findViewById(R.id.pagertitle);
23          
24         //将要分页显示的View装入数组中
25         LayoutInflater mLi = LayoutInflater.from(this);
26         View view1 = mLi.inflate(R.layout.view1, null);
27         View view2 = mLi.inflate(R.layout.view2, null);
28         View view3 = mLi.inflate(R.layout.view3, null);
29          
30         //每个页面的Title数据
31         final ArrayList<View> views = new ArrayList<View>();
32         views.add(view1);
33         views.add(view2);
34         views.add(view3);
35          
36         final ArrayList<String> titles = new ArrayList<String>();
37         titles.add("tab1");
38         titles.add("tab2");
39         titles.add("tab3");
40          
41         //填充ViewPager的数据适配器
42         PagerAdapter mPagerAdapter = new PagerAdapter() {
43              
44             @Override
45             public boolean isViewFromObject(View arg0, Object arg1) {
46                 return arg0 == arg1;
47             }
48              
49             @Override
50             public int getCount() {
51                 return views.size();
52             }
53  
54             @Override
55             public void destroyItem(View container, int position, Object object) {
56                 ((ViewPager)container).removeView(views.get(position));
57             }
58  
59             @Override
60             public CharSequence getPageTitle(int position) {
61                 return titles.get(position);
62             }
63  
64             @Override
65             public Object instantiateItem(View container, int position) {
66                 ((ViewPager)container).addView(views.get(position));
67                 return views.get(position);
68             }
69         };
70          
71         mViewPager.setAdapter(mPagerAdapter);
72     }
73 }
viewpager使用_第1张图片

你可能感兴趣的:(viewpager使用)