先看一下效果图
上代码
implementation 'com.xhb:xbanner:1.5.1'
implementation 'com.github.bumptech.glide:glide:4.11.0'
<com.stx.xhb.xbanner.XBanner
android:id="@+id/xbander"
app:isAutoPlay="true"
app:isShowNumberIndicator="true"
app:isClipChildrenMode="true"
android:layout_width="match_parent"
android:layout_height="150dp"/>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取id
XBanner xbander= findViewById(R.id.xbander);
//添加轮播图
final List<String> image = new ArrayList<>();
image.add("https://seopic.699pic.com/photo/40193/3766.jpg_wh1200.jpg");
image.add("https://seopic.699pic.com/photo/40192/9604.jpg_wh1200.jpg");
image.add("https://seopic.699pic.com/photo/40193/2934.jpg_wh1200.jpg");
image.add("https://seopic.699pic.com/photo/40193/0024.jpg_wh1200.jpg");
xbander.setData(image, null);
xbander.loadImage(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
Glide.with(MainActivity.this).load(image.get(position)).into((ImageView) view);
}
});
//点击图片
xbander.setOnItemClickListener(new XBanner.OnItemClickListener() {
@Override
public void onItemClick(XBanner banner, Object model, View view, int position) {
Toast.makeText(MainActivity.this, "点击了第"+(position+1)+"张图片", Toast.LENGTH_SHORT).show();
}
});
}
}
如果编译报错 那么就尝试一下
添加一行代码
android.enableJetifier=true