package com.example.day_12_viewpager;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
String[] path = new String[] {
“http://imgs.juheapi.com/comic_xin/zbfOxNfWRA==/5010/6-NTAxMDY=.jpg“,
“http://imgs.juheapi.com/comic_xin/zbfOxNfWRA==/5010/16-NTAxMDE2.jpg“,
“http://imgs.juheapi.com/comic_xin/zbfOxNfWRA==/5010/23-NTAxMDIz.jpg“,
“http://imgs.juheapi.com/comic_xin/zbfOxNfWRA==/5010/59-NTAxMDU5.jpg” };
private LinearLayout ll;
//記錄小圓點的位置
int lastIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager vp = (ViewPager) findViewById(R.id.vp);
ll = (LinearLayout) findViewById(R.id.ll);
vp.setAdapter(new MyPagerAdapter(path, this));
vp.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2
% path.length);
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
//取出正確的角標
int index = arg0 % path.length;
//取出LinearLayout裏當前的imageView
ImageView imageView = (ImageView) ll.getChildAt(index);
imageView.setEnabled(false);
ImageView lastImageView = (ImageView) ll.getChildAt(lastIndex);
lastImageView.setEnabled(true);
//記錄當前圓點位置,做爲下一次執行onPageSelected方法,變黑的ImageView的位置
lastIndex=index;
}
@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
}
});
setPonit();
}
/**
* 添加小圓點
*/
private void setPonit() {
// 小圓點要與圖片的個數一致
for (int i = 0; i < path.length; i++) {
// 創建imageView
ImageView imageView = new ImageView(this);
// 設置小圓點的資源文件
imageView.setBackgroundResource(R.drawable.backcolor);
// LayoutParams對象的類型,取決於該控件的父控件類型
LayoutParams layoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// 設置有右間距
layoutParams.rightMargin = 15;
// 設置imageView的屬性
imageView.setLayoutParams(layoutParams);
//初始化第一個小點的顏色
if (i == 0) {
imageView.setEnabled(false);
}
// 把imageView添加到父控件
ll.addView(imageView);
}
}
}
//適配器
/**
*
*/
package com.example.day_12_viewpager;
import com.lidroid.xutils.BitmapUtils;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
* @author WJL
*
*/
public class MyPagerAdapter extends PagerAdapter {
String[] path;
Context context;
BitmapUtils bitmapUtils;
/**
* @param path
*/
public MyPagerAdapter(String[] path, Context context) {
this.path = path;
this.context = context;
// Xutils裏請求網絡圖片的工具類
bitmapUtils = new BitmapUtils(context);
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int i = position % path.length;
bitmapUtils.display(imageView, path[i]);
container.addView(imageView);
return imageView;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
container.removeView((View) object);
}
}
//XML文件
xmlns:android=”http://schemas.android.com/apk/res/android”
android:shape=”oval”
>
<solid android:color="#000"/>
xmlns:android=”http://schemas.android.com/apk/res/android”
android:shape=”oval”
>
<solid android:color="#f00"/>
<item android:state_enabled="true" android:drawable="@drawable/enabledtrue"></item>
<item android:state_enabled="false" android:drawable="@drawable/enabledfalse"></item>