### 首先XBanner的依赖
//在build.gradle文件中加入依赖
compile 'com.xhb:xbanner:1.2.2'
compile 'com.nineoldandroids:library:2.4.0'
//要导入一些图片加载框架的依赖,如Glide
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'jp.wasabeef:glide-transformations:1.0.6'
### 布局页面
<com.stx.xhb.xbanner.XBanner
android:id="@+id/san_xbanner"
android:layout_width="match_parent"
android:layout_height="390dp"
app:AutoPlayTime="3000"
app:pointsContainerBackground="#44AAAAAA"
app:pointsPosition="CENTER"
app:tipTextColor="#FFFFFFFF"
app:tipTextSize="16.0sp"
></com.stx.xhb.xbanner.XBanner>
### Activity的主流代码
public class SanActivity extends AppCompatActivity {
private XBanner xBanner;
private Button san_cart;
private Button san_addcart;
String pid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_san);
//获取资源ID
xBanner = findViewById(R.id.san_xbanner);
san_cart = findViewById(R.id.san_cart);
san_addcart = findViewById(R.id.san_addcart);
//接收pid的值
Intent intent = getIntent();
pid = intent.getStringExtra("pid");
//接收images的值图片
String images = getIntent().getStringExtra("images");
final List<String> stringB = Arrays.asList(images.split("\\|"));
// 为XBanner绑定数据
xBanner.setData(stringB,null);
// XBanner适配数据
xBanner.setmAdapter(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, View view, int position) {
Glide.with(SanActivity.this).load(stringB.get(position)).into((ImageView) view);
}
});
//设置添加购物车的点击事件
san_addcart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://www.zhaoapi.cn/product/addCart&uid=71&pid="+pid+"";
Okutils.getOkutils().doget(url, new OkCallback() {
@Override
public void success(String json) {
Toast.makeText(SanActivity.this,"添加成功",Toast.LENGTH_LONG).show();
}
@Override
public void failed(String json) {
Toast.makeText(SanActivity.this,"添加失败",Toast.LENGTH_LONG).show();
}
});
}
});
//设置san_but04购物车点击跳转详情页面
san_cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(SanActivity.this,SiActivity.class);
startActivity(it);
}
});
}
}