在使用之前要加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. */
- @Override
- public 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;
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- mPager.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) {
- }
- @Override
- public int getCount() {
- return mListViews.size();
- }
- @Override
- public Object instantiateItem(View arg0, int arg1) {
- ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
- return mListViews.get(arg1);
- }
- @Override
- public boolean isViewFromObject(View arg0, Object arg1) {
- return arg0 == (arg1);
- }
- @Override
- public void restoreState(Parcelable arg0, ClassLoader arg1) {
- }
- @Override
- public Parcelable saveState() {
- return null;
- }
- @Override
- public void startUpdate(View arg0) {
- }
- @Override
- public void onClick(View v) {
- AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
- .setIcon(null)
- .setTitle("dialog")
- .setMessage("nihao")
- .setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface arg0,
- int arg1) {
- MainActivity.this.finish();
- }
- })
- .setNegativeButton("取消",
- new DialogInterface.OnClickListener() {
- @Override
- public 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 偏移量
- @Override
- public 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);
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- }
- }
- }
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. */
@Override
public 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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPager.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) {
}
@Override
public int getCount() {
return mListViews.size();
}
@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(mListViews.get(arg1), 0);
return mListViews.get(arg1);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == (arg1);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
@Override
public void onClick(View v) {
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setIcon(null)
.setTitle("dialog")
.setMessage("nihao")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
MainActivity.this.finish();
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public 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 偏移量
@Override
public 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);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public 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>
<?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>
<?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>
<?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>
<?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>