android viewpage的使用


 

android viewpage的使用


在使用之前要加support.v4包哦,一般在D:\android-sdk-windows\extras\android\support\v4目录下面,好像4.0以后见工程的时候自动加载的......

下面贴代码及效果图:


mainactivity类代码如下:

[html]  view plain copy print ?
  1. package com.xy.viewpager;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.os.Bundle;  
  7. import android.os.Parcelable;  
  8. import android.support.v4.view.PagerAdapter;  
  9. import android.support.v4.view.ViewPager;  
  10. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  11. import android.util.DisplayMetrics;  
  12. import android.util.Log;  
  13. import android.view.LayoutInflater;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.view.animation.Animation;  
  17. import android.view.animation.TranslateAnimation;  
  18. import android.widget.Button;  
  19. import android.widget.ImageView;  
  20. import android.widget.RelativeLayout;  
  21. import android.widget.TextView;  
  22. import android.app.Activity;  
  23. import android.app.AlertDialog;  
  24. import android.content.DialogInterface;  
  25. import android.graphics.BitmapFactory;  
  26.   
  27. public class MainActivity extends Activity {  
  28.     private ViewPager mPager;// 页卡内容  
  29.     private List<View> listViews; // Tab页面列表  
  30.     private ImageView cursor;// 动画图片  
  31.     private TextView t1, t2, t3;// 页卡头标  
  32.     private int offset = 0;// 动画图片偏移量  
  33.     private int currIndex = 0;// 当前页卡编号  
  34.     private int bmpW;// 动画图片宽度  
  35.     MyPagerAdapter adapter;  
  36.     LayoutInflater mInflater;  
  37.   
  38.     RelativeLayout rel;  
  39.   
  40.     /** Called when the activity is first created. */  
  41.     @Override  
  42.     public void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.activity_main);  
  45.         Log.i("Viewpage", "--onCreate--");  
  46.         initImageView();  
  47.         initTextView();  
  48.         initPageView();  
  49.   
  50.     }  
  51.   
  52.     private void initPageView() {  
  53.         mInflater = getLayoutInflater();  
  54.         listViews = new ArrayList<View>();  
  55.         listViews.add(mInflater.inflate(R.layout.layou1, null));  
  56.         listViews.add(mInflater.inflate(R.layout.layou2, null));  
  57.         listViews.add(mInflater.inflate(R.layout.layou3, null));  
  58.         adapter = new MyPagerAdapter(listViews);  
  59.         mPager = (ViewPager) findViewById(R.id.page);  
  60.         mPager.setAdapter(adapter);  
  61.         mPager.setCurrentItem(0);  
  62.         mPager.setOnPageChangeListener(new MyOnPageChangeListener());  
  63.     }  
  64.   
  65.     private void initTextView() {  
  66.         t1 = (TextView) findViewById(R.id.tab1);  
  67.         t2 = (TextView) findViewById(R.id.tab2);  
  68.         t3 = (TextView) findViewById(R.id.tab3);  
  69.         t1.setOnClickListener(new MyOnClickListener(0));  
  70.         t2.setOnClickListener(new MyOnClickListener(1));  
  71.         t3.setOnClickListener(new MyOnClickListener(2));  
  72.     }  
  73.   
  74.     private void initImageView() {  
  75.         cursor = (ImageView) findViewById(R.id.cursor);  
  76.         rel = (RelativeLayout) findViewById(R.id.layout);  
  77.           
  78.   
  79.         bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.png)  
  80.                 .getWidth();  
  81.         DisplayMetrics dm = new DisplayMetrics();  
  82.         getWindowManager().getDefaultDisplay().getMetrics(dm);  
  83.         int screenW = dm.widthPixels;  
  84.         offset = (screenW / 3 - bmpW) / 2;  
  85.         // Matrix matrix = new Matrix();  
  86.         // matrix.postTranslate(offset, 0);  
  87.         cursor.setBackgroundResource(R.drawable.png);  
  88.         // cursor.setScaleType(ScaleType.MATRIX);  
  89.         // cursor.setImageMatrix(matrix);  
  90.         rel.setPadding(offset, 0, 0, 0);  
  91.   
  92.     }  
  93.   
  94.     public class MyOnClickListener implements View.OnClickListener {  
  95.         private int index = 0;  
  96.   
  97.         public MyOnClickListener(int i) {  
  98.             index = i;  
  99.         }  
  100.   
  101.         @Override  
  102.         public void onClick(View v) {  
  103.             // TODO Auto-generated method stub  
  104.             mPager.setCurrentItem(index);  
  105.         }  
  106.     }  
  107.   
  108.     public class MyPagerAdapter extends PagerAdapter implements OnClickListener {  
  109.         public List<View> mListViews;  
  110.         public View v1;  
  111.         public View v2;  
  112.         public View v3;  
  113.         public Button mButton;  
  114.   
  115.         public MyPagerAdapter(List<View> mListViews) {  
  116.             this.mListViews = mListViews;  
  117.             getViewClickListener(mListViews);  
  118.         }  
  119.   
  120.         public void getViewClickListener(List<View> listview) {  
  121.             v1 = listview.get(0);  
  122.             v2 = listview.get(1);  
  123.             v3 = listview.get(2);  
  124.             mButton = (Button) v1.findViewById(R.id.button);  
  125.             mButton.setOnClickListener(this);  
  126.         }  
  127.   
  128.         public void destroyItem(View arg0, int arg1, Object arg2) {  
  129.             ((ViewPager) arg0).removeView(mListViews.get(arg1));  
  130.         }  
  131.   
  132.         public void finishUpdate(View arg0) {  
  133.         }  
  134.   
  135.         @Override  
  136.         public int getCount() {  
  137.             return mListViews.size();  
  138.         }  
  139.   
  140.         @Override  
  141.         public Object instantiateItem(View arg0, int arg1) {  
  142.             ((ViewPager) arg0).addView(mListViews.get(arg1), 0);  
  143.             return mListViews.get(arg1);  
  144.         }  
  145.   
  146.         @Override  
  147.         public boolean isViewFromObject(View arg0, Object arg1) {  
  148.             return arg0 == (arg1);  
  149.         }  
  150.   
  151.         @Override  
  152.         public void restoreState(Parcelable arg0, ClassLoader arg1) {  
  153.         }  
  154.   
  155.         @Override  
  156.         public Parcelable saveState() {  
  157.             return null;  
  158.         }  
  159.   
  160.         @Override  
  161.         public void startUpdate(View arg0) {  
  162.         }  
  163.   
  164.         @Override  
  165.         public void onClick(View v) {  
  166.             AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)  
  167.                     .setIcon(null)  
  168.                     .setTitle("dialog")  
  169.                     .setMessage("nihao")  
  170.                     .setPositiveButton("确定",  
  171.                             new DialogInterface.OnClickListener() {  
  172.   
  173.                                 @Override  
  174.                                 public void onClick(DialogInterface arg0,  
  175.                                         int arg1) {  
  176.   
  177.                                     MainActivity.this.finish();  
  178.   
  179.                                 }  
  180.   
  181.                                 })  
  182.                     .setNegativeButton("取消",  
  183.                             new DialogInterface.OnClickListener() {  
  184.   
  185.                                 @Override  
  186.                                 public void onClick(DialogInterface arg0,  
  187.                                         int arg1) {  
  188.   
  189.                                 }  
  190.   
  191.                             }).create();  
  192.   
  193.             // 显示对话框也可以使用showDialog(int id)方法显示对话框  
  194.   
  195.             dialog.show();  
  196.         }  
  197.     }  
  198.   
  199.     public class MyOnPageChangeListener implements OnPageChangeListener {  
  200.   
  201.         int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量  
  202.         int two = one * 2;// 页卡1 -> 页卡3 偏移量  
  203.   
  204.         @Override  
  205.         public void onPageSelected(int arg0) {  
  206.             Animation animation = null;  
  207.             switch (arg0) {  
  208.             case 0:  
  209.                 if (currIndex == 1) {  
  210.                     animation = new TranslateAnimation(one, 0, 0, 0);  
  211.                 } else if (currIndex == 2) {  
  212.                     animation = new TranslateAnimation(two, 0, 0, 0);  
  213.                 }  
  214.                 break;  
  215.             case 1:  
  216.                 if (currIndex == 0) {  
  217.                     animation = new TranslateAnimation(offset, one, 0, 0);  
  218.                 } else if (currIndex == 2) {  
  219.                     animation = new TranslateAnimation(two, one, 0, 0);  
  220.                 }  
  221.                 break;  
  222.             case 2:  
  223.                 if (currIndex == 0) {  
  224.                     animation = new TranslateAnimation(offset, two, 0, 0);  
  225.                 } else if (currIndex == 1) {  
  226.                     animation = new TranslateAnimation(one, two, 0, 0);  
  227.                 }  
  228.                 break;  
  229.             }  
  230.             currIndex = arg0;  
  231.             animation.setFillAfter(true);// True:图片停在动画结束位置  
  232.             animation.setDuration(300);  
  233.             rel.startAnimation(animation);  
  234.         }  
  235.   
  236.         @Override  
  237.         public void onPageScrolled(int arg0, float arg1, int arg2) {  
  238.               
  239.         }  
  240.   
  241.         @Override  
  242.         public void onPageScrollStateChanged(int arg0) {  
  243.               
  244.         }  
  245.     }  
  246. }  

mainlayout.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/nav"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="50dp"  
  11.         android:background="#efefef" >  
  12.   
  13.         <TextView  
  14.             android:id="@+id/tab1"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="fill_parent"  
  17.             android:layout_weight="1.0"  
  18.             android:gravity="center"  
  19.             android:text="页片1"  
  20.             android:textColor="#000000" />  
  21.   
  22.         <TextView  
  23.             android:id="@+id/tab2"  
  24.             android:layout_width="fill_parent"  
  25.             android:layout_height="fill_parent"  
  26.             android:layout_weight="1.0"  
  27.             android:gravity="center"  
  28.             android:text="页片2"  
  29.             android:textColor="#000000" />  
  30.   
  31.         <TextView  
  32.             android:id="@+id/tab3"  
  33.             android:layout_width="fill_parent"  
  34.             android:layout_height="fill_parent"  
  35.             android:layout_weight="1.0"  
  36.             android:gravity="center"  
  37.             android:text="页片3"  
  38.             android:textColor="#000000" />  
  39.     </LinearLayout>  
  40.   
  41.     <RelativeLayout  
  42.         android:id="@+id/layout"  
  43.         android:layout_width="wrap_content"  
  44.         android:layout_height="wrap_content" >  
  45.   
  46.         <ImageView  
  47.             android:id="@+id/cursor"  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content" />  
  50.     </RelativeLayout>  
  51.   
  52.     <android.support.v4.view.ViewPager  
  53.         android:id="@+id/page"  
  54.         android:layout_width="fill_parent"  
  55.         android:layout_height="fill_parent" >  
  56.     </android.support.v4.view.ViewPager>  
  57.   
  58. </LinearLayout>  

layout1.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#565656"  
  6. android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/button"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="点击事件" />  
  13.   
  14. </LinearLayout>  

layout2.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:layout_width="fill_parent"    
  4.     android:layout_height="fill_parent"    
  5.     android:orientation="vertical"    
  6.     android:background="#abab00">    
  7. </LinearLayout>    

layout3.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#00abcd"  
  6.     android:orientation="vertical" >  
  7.   
  8. </LinearLayout>  




quot;fill_parent

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值