这是控件
因为这个控件是第三方的 所以我们需要加入依赖
<cn.bingoogolapple.bgabanner.BGABanner
android:id="@+id/baner"
android:layout_width=“match_parent”
android:layout_height=“200dp”
/>
BGAbanner依赖
implementation ‘cn.bingoogolapple:bga-banner:2.1.7’
public class MainActivity extends AppCompatActivity {
private BGABanner mBGABanner;
//模拟数据
private String[] picUrl={
“https://img.huxiucdn.com/article/cover/201804/11/142701198959.jpg?imageView2/1/w/710/h/400/|imageMogr2/strip/interlace/1/quality/85/format/jpg”,
“https://img.huxiucdn.com/article/cover/201809/10/195039242422.jpg?imageView2/1/w/710/h/400/|imageMogr2/strip/interlace/1/quality/85/format/jpg”,
“https://img.huxiucdn.com/article/cover/201809/10/143541089263.jpg?imageView2/1/w/710/h/400/|imageMogr2/strip/interlace/1/quality/85/format/jpg”
};
private String[] picDes={
“资讯拼多多?趣头条不值得高估”,
“马云接班人为什么是张勇”,
“《天天德州》终下架:面对赌博灰产,腾讯也无能为力”
};
private List<String> imgList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBGABanner=(BGABanner)findViewById(R.id.baner);
imgList=new ArrayList<>();
List<String> imgDesc=new ArrayList<>();
for(int a=0;a<picUrl.length;a++){
imgList.add(picUrl[a]);
imgDesc.add(picDes[a]);
}
mBGABanner.setData(imgList,imgDesc);
//适配器
mBGABanner.setAdapter(new BGABanner.Adapter<ImageView,String>() {
@Override
public void fillBannerItem(BGABanner banner, ImageView itemView, @Nullable String model, int position) {
Picasso.with(MainActivity.this).load(imgList.get(position)).fit().into(itemView);
}
});
}
}
**
一般情况下都是使用的网络工具类获取的数据 所以一定 要把Banner配置一些数据放到Gson解析的下面 不然会报 空指针的
**