关闭

无限轮播+小圆点

68人阅读 评论(0) 收藏 举报
private ViewPager vp;
private LinearLayout ll;
//设置小圆点
int listIndex;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
int item = vp.getCurrentItem();
int a=item+1;
vp.setCurrentItem(a);
handler.sendEmptyMessageDelayed(0, 2000);

};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


//设置小圆点
sollPoint();

//设置初始页面
vp.setCurrentItem(Integer.MAX_VALUE/2-Integer.MAX_VALUE/2%vpimg.size());
//设配器
Adapter adapter=new Adapter(this, vpimg);
vp.setAdapter(adapter);

//移除所有的信息重新输入
handler.removeCallbacksAndMessages(null);
handler.sendEmptyMessageDelayed(0, 2000);

//ViewPager的监听事件
vp.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
//取正确的角标
int index=arg0%vpimg.size();
ImageView img=(ImageView) ll.getChildAt(index);
img.setEnabled(true);
ImageView lastimg=(ImageView) ll.getChildAt(listIndex);
lastimg.setEnabled(false);
listIndex=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

}
});


}








//加载小圆点
public void sollPoint(){
//小圆点的个数要与图片个数相同
for(int i=0;i<vpimg.size();i++){
//创建InageView
ImageView img=new ImageView(this);
//设置小圆点在资源文件
img.setBackgroundResource(R.drawable.zonghe);
LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//设置右间距
lp.rightMargin=15;
//设置图片属性
img.setLayoutParams(lp);
//初始化第一个小点
if(i==0){
img.setEnabled(true);
}else{
img.setEnabled(false);
}

ll.addView(img);
}


}




//轮播的适配器
public class Adapter extends PagerAdapter{

Context context;
List<Item> vpimg=new ArrayList<Item>();




public Adapter(Context context, List<Item> vpimg) {
super();
this.context = context;
this.vpimg = vpimg;
}


@Override
public int getCount() {
return Integer.MAX_VALUE;
}


@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}


@Override
public Object instantiateItem(ViewGroup container, int position) {
View v=View.inflate(context, R.layout.item_vp, null);
ImageView img=(ImageView) v.findViewById(R.id.img);
ImageLoader imager=ImageLoader.getInstance();
imager.displayImage(vpimg.get(position%vpimg.size()).getImg(), img);
container.addView(v);

return v;
}




}








//小圆点的选择状态
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    
    <size android:width="5dp" android:height="5dp"/>
    <solid android:color="#f00"/>
    


</shape>






//小圆点未选择状态
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    
    <size android:width="5dp" android:height="5dp"/>
    <solid android:color="#000"/>


</shape>






//小圆点综合在一起
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item android:state_enabled="false" android:drawable="@drawable/efalse"></item>
<item android:state_enabled="true" android:drawable="@drawable/etrue"></item>
    
</selector>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3867次
    • 积分:284
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条