在使用之前要加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<View> 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<View>();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<View> mListViews;public View v1;public View v2;public View v3;public Button mButton;public MyPagerAdapter(List<View> mListViews) {this.mListViews = mListViews;getViewClickListener(mListViews);}public void getViewClickListener(List<View> 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"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <LinearLayout        android:id="@+id/nav"        android:layout_width="fill_parent"        android:layout_height="50dp"        android:background="#efefef" >        <TextView            android:id="@+id/tab1"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_weight="1.0"            android:gravity="center"            android:text="页片1"            android:textColor="#000000" />        <TextView            android:id="@+id/tab2"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_weight="1.0"            android:gravity="center"            android:text="页片2"            android:textColor="#000000" />        <TextView            android:id="@+id/tab3"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_weight="1.0"            android:gravity="center"            android:text="页片3"            android:textColor="#000000" />    </LinearLayout>    <RelativeLayout        android:id="@+id/layout"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <ImageView            android:id="@+id/cursor"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </RelativeLayout>    <android.support.v4.view.ViewPager        android:id="@+id/page"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >    </android.support.v4.view.ViewPager></LinearLayout>

layout1.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#565656"android:orientation="vertical" >    <Button        android:id="@+id/button"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="点击事件" /></LinearLayout>

layout2.xml:

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="vertical"      android:background="#abab00">  </LinearLayout>  

layout3.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#00abcd"    android:orientation="vertical" ></LinearLayout>




quot;fill_parent

更多相关文章

  1. Android(安卓)Systrace
  2. android源代码编译
  3. android 自定义对话框
  4. 整理出15个Android很有用的代码片段
  5. Android简单的Timer小例子
  6. android中使用特殊符号
  7. android 2.3 4.0 添加开机音乐
  8. Android(安卓)如何通过menu id来得到menu item 控件
  9. Android(安卓)WebView 实例

随机推荐

  1. AndroidStudio练习用--登录Demo
  2. Android———从GitHub上下载源码的方法
  3. 通过WifiManager可以实现对wifi进行操作,
  4. 【android之锚定视图】
  5. Android(安卓)自定义底部导航栏
  6. android弹出式菜单(效果爆炸)
  7. Android文本框实现搜索和清空效果
  8. Android(安卓)apk反编译java代码
  9. Android(安卓)中文API (69) ―― Bluetooth
  10. XUI 一个简洁而优雅的Android原生UI框架,