import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
public class MainActivity extends Activity {
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int index = msg.what;
viewPager.setCurrentItem(index -= 1);
int i = viewPager.getCurrentItem();
handler.sendEmptyMessageDelayed(i, 2000);
};
};
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<String> list = new ArrayList<String>();
list.add("http://t12.baidu.com/it/u=176596478,1719464859&fm=59");
list.add("http://t10.baidu.com/it/u=4010714787,2970343447&fm=59");
list.add("http://t11.baidu.com/it/u=2520234337,3270250852&fm=59");
list.add("http://t10.baidu.com/it/u=2395220391,4053197141&fm=59");
viewPager = (ViewPager) findViewById(R.id.viewPager);
// 5 - 5%4
// int current =10/2 - 10/2%list.size();
int current = Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2
% list.size();
viewPager.setAdapter(new MyPagerAdpter(this, list));
viewPager.setCurrentItem(current);
int currentItem = viewPager.getCurrentItem();
handler.sendEmptyMessageDelayed(currentItem, 2000);
}
}
适配器
/**
*
*/
package com.example.day_15_;
import java.util.List;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.lidroid.xutils.BitmapUtils;
/**
* @author WJL
*
*/
public class MyPagerAdpter extends PagerAdapter {
List<String> urls;
Context context;
public MyPagerAdpter(Context context, List<String> urls) {
this.urls = urls;
this.context = context;
}
@Override
// 实例化条目
public Object instantiateItem(ViewGroup container, int position) {
Log.i("http_", position+"");
ImageView imageView = new ImageView(context);
BitmapUtils bitmapUtils = new BitmapUtils(context);
bitmapUtils.display(imageView, urls.get(position%urls.size()));
container.addView(imageView);
return imageView;
}
@Override
// 总数
public int getCount() {
// TODO Auto-generated method stub
// return urls.size();
return Integer.MAX_VALUE;
// return 10;
}
@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文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</RelativeLayout>