在使用之前要加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>



android viewpage的使用_第1张图片


quot;fill_parent

更多相关文章

  1. Android使用代码实现关机/重启
  2. Android 将activity设置成对话框样式
  3. 【知识点】android代码中设置margin
  4. Android蓝牙通信代码
  5. Android代码规范
  6. android典型代码系列(二十二)------按键使上面的EditText抖动

随机推荐

  1. ImageView的android:maxHeight,android:mi
  2. Pro Andorid3第一章:Android平台简介
  3. android 旋转动画
  4. cocos2d-x的win32工程移植到Android
  5. System Server 分析
  6. android 上调试动态库方法
  7. Android(安卓)kernel 编译
  8. android:shape的使用
  9. Android应用程序的数据存放目录 路径
  10. Android(安卓)Gradle Plugin指南(一)——简