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

下面贴代码及效果图:


mainactivity类代码如下:

package com.xy.viewpager;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.os.Parcelable;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.util.DisplayMetrics;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.graphics.BitmapFactory;public class MainActivity extends Activity {private ViewPager mPager;// 页卡内容private List listViews; // Tab页面列表private ImageView cursor;// 动画图片private TextView t1, t2, t3;// 页卡头标private int offset = 0;// 动画图片偏移量private int currIndex = 0;// 当前页卡编号private int bmpW;// 动画图片宽度MyPagerAdapter adapter;LayoutInflater mInflater;RelativeLayout rel;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.i("Viewpage", "--onCreate--");initImageView();initTextView();initPageView();}private void initPageView() {mInflater = getLayoutInflater();listViews = new ArrayList();listViews.add(mInflater.inflate(R.layout.layou1, null));listViews.add(mInflater.inflate(R.layout.layou2, null));listViews.add(mInflater.inflate(R.layout.layou3, null));adapter = new MyPagerAdapter(listViews);mPager = (ViewPager) findViewById(R.id.page);mPager.setAdapter(adapter);mPager.setCurrentItem(0);mPager.setOnPageChangeListener(new MyOnPageChangeListener());}private void initTextView() {t1 = (TextView) findViewById(R.id.tab1);t2 = (TextView) findViewById(R.id.tab2);t3 = (TextView) findViewById(R.id.tab3);t1.setOnClickListener(new MyOnClickListener(0));t2.setOnClickListener(new MyOnClickListener(1));t3.setOnClickListener(new MyOnClickListener(2));}private void initImageView() {cursor = (ImageView) findViewById(R.id.cursor);rel = (RelativeLayout) findViewById(R.id.layout);bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.png).getWidth();DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);int screenW = dm.widthPixels;offset = (screenW / 3 - bmpW) / 2;// Matrix matrix = new Matrix();// matrix.postTranslate(offset, 0);cursor.setBackgroundResource(R.drawable.png);// cursor.setScaleType(ScaleType.MATRIX);// cursor.setImageMatrix(matrix);rel.setPadding(offset, 0, 0, 0);}public class MyOnClickListener implements View.OnClickListener {private int index = 0;public MyOnClickListener(int i) {index = i;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubmPager.setCurrentItem(index);}}public class MyPagerAdapter extends PagerAdapter implements OnClickListener {public List mListViews;public View v1;public View v2;public View v3;public Button mButton;public MyPagerAdapter(List mListViews) {this.mListViews = mListViews;getViewClickListener(mListViews);}public void getViewClickListener(List listview) {v1 = listview.get(0);v2 = listview.get(1);v3 = listview.get(2);mButton = (Button) v1.findViewById(R.id.button);mButton.setOnClickListener(this);}public void destroyItem(View arg0, int arg1, Object arg2) {((ViewPager) arg0).removeView(mListViews.get(arg1));}public void finishUpdate(View arg0) {}@Overridepublic int getCount() {return mListViews.size();}@Overridepublic Object instantiateItem(View arg0, int arg1) {((ViewPager) arg0).addView(mListViews.get(arg1), 0);return mListViews.get(arg1);}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == (arg1);}@Overridepublic void restoreState(Parcelable arg0, ClassLoader arg1) {}@Overridepublic Parcelable saveState() {return null;}@Overridepublic void startUpdate(View arg0) {}@Overridepublic void onClick(View v) {AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setIcon(null).setTitle("dialog").setMessage("nihao").setPositiveButton("确定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0,int arg1) {MainActivity.this.finish();}}).setNegativeButton("取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0,int arg1) {}}).create();// 显示对话框也可以使用showDialog(int id)方法显示对话框dialog.show();}}public class MyOnPageChangeListener implements OnPageChangeListener {int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量int two = one * 2;// 页卡1 -> 页卡3 偏移量@Overridepublic void onPageSelected(int arg0) {Animation animation = null;switch (arg0) {case 0:if (currIndex == 1) {animation = new TranslateAnimation(one, 0, 0, 0);} else if (currIndex == 2) {animation = new TranslateAnimation(two, 0, 0, 0);}break;case 1:if (currIndex == 0) {animation = new TranslateAnimation(offset, one, 0, 0);} else if (currIndex == 2) {animation = new TranslateAnimation(two, one, 0, 0);}break;case 2:if (currIndex == 0) {animation = new TranslateAnimation(offset, two, 0, 0);} else if (currIndex == 1) {animation = new TranslateAnimation(one, two, 0, 0);}break;}currIndex = arg0;animation.setFillAfter(true);// True:图片停在动画结束位置animation.setDuration(300);rel.startAnimation(animation);}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}@Overridepublic void onPageScrollStateChanged(int arg0) {}}}

mainlayout.xml:

<?xml version="1.0" encoding="utf-8"?>                                                        

layout1.xml:

<?xml version="1.0" encoding="utf-8"?>    

layout2.xml:

<?xml version="1.0" encoding="utf-8"?>      

layout3.xml:

<?xml version="1.0" encoding="utf-8"?>




quot;fill_parent

更多相关文章

  1. Android——最新LitePal使用
  2. 通过ijetty获取android设备的截图
  3. android中开机自动运行程序
  4. Android下载网络文本
  5. React Native For Android初探
  6. 用Android代码实现自动打开USB调试
  7. android onTouchEvent和setOnTouchListener中onTouch的区别
  8. Android使用webview让服务器上的js调用java代码的方法
  9. Android开发实践 界面编程(中)

随机推荐

  1. 一个测试ok的popupwindow demo
  2. Android2.2 控件列表和层次
  3. startService写法
  4. Android实际开发bug大总结
  5. Android(安卓)通过findViewById方式创建T
  6. android 监听方法
  7. Android(安卓)File 数据存储
  8. Android(安卓)判断是否得到 root权限
  9. 【Android】柱状图示例
  10. 关于Android(安卓)Studio3.0使用过程的一