借鉴文章:
http://www.imooc.com/article/251457
依赖:
implementation 'com.xhb:xbanner:1.0.0'
布局:
注意:layout_height不可用wrap_content
<com.stx.xhb.xbanner.XBanner
android:id="@+id/XBanner"
android:layout_width="match_parent"
android:layout_height="250dp"
app:AutoPlayTime="1500"
/>
主要用到方法有setData()、setmAdapter():
List<String> mXbannerList;
String image = data.getImages().split("\\|")[0].replace("https","http");
String image1 = data.getImages().split("\\|")[1].replace("https","http");
String image2 = data.getImages().split("\\|")[2].replace("https","http");
mXbannerList=new ArrayList<>();
mXbannerList.add(image);
mXbannerList.add(image1);
mXbannerList.add(image2);
//1:xbanner初始化图片集合
mXBanner.setData(mXbannerList);
mXBanner.setmAdapter(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, View view, int position) {
//2:Glide加载图片
Glide.with(MainActivity.this).load(mXbannerList.get(position)).into((ImageView) view);
}
});
Fresco形式:
//R.layout.home_fragment_xbanner为定义的布局
mXBanner.setData(R.layout.home_fragment_xbanner,mXbannerList,null);
mXBanner.setmAdapter(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
//R.id.home_xbanner_swdv为SDV的id
SimpleDraweeView simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.home_xbanner_swdv);
simpleDraweeView.setImageURI(mXbannerList.get(position));
}
});
## R.layout.home_fragment_xbanner:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fresco="http://schemas.android.com/apk/res-auto">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/home_xbanner_swdv"
android:layout_width="match_parent"
android:layout_height="match_parent"
fresco:placeholderImage="@mipmap/index_pic4"
/>
</RelativeLayout>
可加载到布局中的属性: