viewpager无限轮播

package com.example.day_15_;


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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值