本文实例为大家分享了convinientbanner顶部广告轮播控件的具体代码,供大家参考,具体内容如下
gradle中添加
compile 'com.bigkoo:convenientbanner:2.0.5'
布局
<com.bigkoo.convenientbanner.ConvenientBanner
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/convenientBanner"
android:layout_width="match_parent"
android:layout_height="200dp"
app:canLoop="true"//是否循环
/>
activity中
public class MainActivity extends AppCompatActivity {
private String[] images = {"http://img2.imgtn.bdimg.com/it/u=3093785514,1341050958&fm=21&gp=0.jpg",
"/file/upload/202206/06/bivohplc2i0.jpg",
"/file/upload/202206/06/p0vjex2eync.jpg",
"/file/upload/202206/06/sqtikjyo2se.jpg",
"/file/upload/202206/06/ycb3in45scd.jpg",
"/file/upload/202206/06/y43zlkpxkbz.gif",
"/file/upload/202206/06/odjdbu4rqjd.gif"
};
//轮播下面的小点
private int[] indicator={R.drawable.icon_selected,R.drawable.icon_unselected};
private ConvenientBanner convenientBanner;
//图片加载地址的集合
private List<String> bean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
convenientBanner = (ConvenientBanner) findViewById(R.id.convenientBanner);
bean= Arrays.asList(images);
convenientBanner.setPointViewVisible(true)
//设置小点
.setPageIndicator(indicator);
//允许手动轮播
convenientBanner.setManualPageable(true);
//设置自动轮播的时间
convenientBanner.startTurning(3000);
//设置点击事件
//泛型为具体实现类ImageLoaderHolder
convenientBanner.setPages(new CBViewHolderCreator<NetImageLoadHolder>() {
@Override
public NetImageLoadHolder createHolder() {
return new NetImageLoadHolder();
}
},bean);
//设置每个pager的点击事件
convenientBanner.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int position) {
Toast.makeText(MainActivity.this, "点击了"+convenientBanner.getCurrentItem(), Toast.LENGTH_SHORT).show();
}
});
}
public class NetImageLoadHolder implements Holder<String>{
private ImageView image_lv;
//可以是一个布局也可以是一个Imageview
@Override
public ImageView createView(Context context) {
image_lv=new ImageView(context);
image_lv.setScaleType(ImageView.ScaleType.FIT_XY);
return image_lv;
}
@Override
public void UpdateUI(Context context, int position, String data) {
Glide.with(context).load(data).placeholder(R.mipmap.ic_launcher).into(image_lv);
}
您可能感兴趣的文章:android实现banner轮播图无限轮播效果Android ViewPager实现无限循环轮播广告位Banner效果Android UI实现广告Banner轮播效果Android自动播放Banner图片轮播效果Android实现Banner界面广告图片循环轮播(包括实现手动滑动循环)Android实现图片轮播效果的两种方法Android实现图片轮播效果Android 使用ViewPager自动滚动循环轮播效果Android实现图片自动轮播并且支持手势左右无限滑动Android自定义Banner轮播效果