先看布局文件:
//注意xmlns的声明
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:layout_height="150dp"
android:background="#248BD4">
android:layout_width="fill_parent"
android:layout_height="140dp"
android:layout_gravity="center" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dip"
app:count="2"
app:point_normal_color="#196194"
app:point_radius="3dp"
app:point_seleted_color="#ffffff"
app:point_size="5dip"
app:space="12dip" />
再写两个用于滚动内容的layout:vp_one,vp_two
android:layout_height="wrap_content"
android:background="@drawable/viewpager_bg"
android:orientation="vertical" >
android:layout_height="40dp"
android:gravity="center_horizontal|top"
android:text="123"
android:textColor="@color/white"
android:textSize="20sp" />
//haha-------------------------
android:layout_height="wrap_content"
android:background="@drawable/viewpager_bg"
android:orientation="vertical" >
android:layout_height="40dp"
android:gravity="center_horizontal|top"
android:text="234"
android:textColor="@color/white"
android:textSize="20sp" />
//在values文件夹下创建attrs.xml文件,java中用的:
//自定义view用于翻滚:
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.example.viewpagertest.R;
public class FlowIndicator extends View {
private static final boolean DEBUG = true;
private int count;
private float space, radius;
private int point_normal_color, point_seleted_color;
// 选中
private int seleted = 0;
// background seleted normal
public FlowIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FlowIndicator);
count = a.getInteger(R.styleable.FlowIndicator_count, 4);
space = a.getDimension(R.styleable.FlowIndicator_space, 9);
radius = a.getDimension(R.styleable.FlowIndicator_point_radius, 9);
point_normal_color = a.getColor(
R.styleable.FlowIndicator_point_normal_color, 0x000000);
point_seleted_color = a.getColor(
R.styleable.FlowIndicator_point_seleted_color, 0xffff07);
int sum = attrs.getAttributeCount();
if (DEBUG) {
String str = "";
for (int i = 0; i < sum; i++) {
String name = attrs.getAttributeName(i);
String value = attrs.getAttributeValue(i);
str += "attr_name :" + name + ": " + value + "\n";
}
Log.i("attribute", str);
}
a.recycle();
}
public void setSeletion(int index) {
this.seleted = index;
invalidate();
}
public void setCount(int count) {
this.count = count;
invalidate();
}
public void next() {
if (seleted < count - 1)
seleted++;
else
seleted = 0;
invalidate();
}
public void previous() {
if (seleted > 0)
seleted--;
else
seleted = count - 1;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
float width = (getWidth() - ((radius * 2 * count) + (space * (count - 1)))) / 2.f;
for (int i = 0; i < count; i++) {
if (i == seleted)
paint.setColor(point_seleted_color);
else
paint.setColor(point_normal_color);
canvas.drawCircle(width + getPaddingLeft() + radius + i
* (space + radius + radius), getHeight() / 2, radius, paint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) (getPaddingLeft() + getPaddingRight()
+ (count * 2 * radius) + (count - 1) * radius + 1);
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) (2 * radius + getPaddingTop() + getPaddingBottom() + 1);
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
}
//main中的代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.viewpagertest.view.FlowIndicator;
public class MainActivity extends Activity {
private ViewPager mViewPager;
private FlowIndicator mIndicator;
private List
private MyViewPagerAdapter mViewPagerAdapter;
private int random;
private static final int CHANGE_VP = 0;
private Timer mTimer;
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case CHANGE_VP:
int item = msg.arg1%2;
mViewPager.setCurrentItem(item,false);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
}
@Override
protected void onResume() {
if(mTimer==null){ //保证只有一个 定时任务
mTimer = new Timer(true);
mTimer.schedule(new MyTask(), 5000, 5000);
}
super.onResume();
}
@Override
protected void onStop() {
if(mTimer!=null){
mTimer.cancel();
mTimer = null;
}
super.onStop();
}
private class MyTask extends TimerTask {
@Override
public void run() {
random++;
Message msg = Message.obtain();
msg.what = CHANGE_VP;
msg.arg1 = random;
handler.sendMessage(msg);
}
}
private void findView() {
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mIndicator = (FlowIndicator) findViewById(R.id.mIndicator);
LayoutInflater inflater = LayoutInflater.from(this);
View recNumView = inflater.inflate(R.layout.vp_dial_item, null);
View recMarkView = inflater.inflate(R.layout.vp_mark_item, null);
viewList.add(recNumView);
viewList.add(recMarkView);
mViewPagerAdapter = new MyViewPagerAdapter(viewList);
mViewPager.setAdapter(mViewPagerAdapter);
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
mIndicator.setSeletion(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
public class MyViewPagerAdapter extends PagerAdapter {
private List
public MyViewPagerAdapter(List
this.myViewList = mListViews;// 构造方法,参数是我们的页卡,这样比较方便。
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(myViewList.get(position));// 删除页卡
}
@Override
public Object instantiateItem(ViewGroup container, int position) { // 这个方法用来实例化页卡
container.addView(myViewList.get(position), 0);// 添加页卡
return myViewList.get(position);
}
@Override
public int getCount() {
return myViewList.size();// 返回页卡的数量
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;// 官方提示这样写
}
}
}
//在各自真实的项目中任意修改完善
另可参考:http://www.tuicool.com/articles/eqmQre