//bean 类
public class Bean {
//news的bean类
//把解析出来的数据根据bean类里的方式进行存储
//必须一一对应
private int status;
private String info;
private List<DataBean> data;
//返回成功的吗 用来进行判断是否成功返回
public boolean isSuccess(){
return status ==1;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String news_id;
private String news_title;
private String news_summary;
private String pic_url;
public String getNews_id() {
return news_id;
}
public void setNews_id(String news_id) {
this.news_id = news_id;
}
public String getNews_title() {
return news_title;
}
public void setNews_title(String news_title) {
this.news_title = news_title;
}
public String getNews_summary() {
return news_summary;
}
public void setNews_summary(String news_summary) {
this.news_summary = news_summary;
}
public String getPic_url() {
return pic_url;
}
public void setPic_url(String pic_url) {
this.pic_url = pic_url;
}
}
}
//轮播
public class MusicFragment extends Fragment {
//初始化banner
private TextView textView;
private Banner banner;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.music,container,false);
//获取资源ID
textView = view.findViewById(R.id.music);
banner = view.findViewById(R.id.MyBanner);
//设置内置样式
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
//设置图片加载器,图片加载器在下方
banner.setImageLoader(new ImageLoaderInterface() {
@Override
public void displayImage(Context context, Object path, View imageView) {
//设置图片
BannerBean.DataBean bean = (BannerBean.DataBean) path;
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(bean.getImg(), (ImageView) imageView);
}
@Override
public View createImageView(Context context) {
//设置图片视图
ImageView imageView = new ImageView(context);
//压缩图片
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//返回值
return imageView;
}
});
//调用方法
initData();
return view;
}
private void initData(){
NetUtils.getInstance().getqusert("http://www.xieast.com/api/banner.php", BannerBean.class, new NetUtils.CallBak<BannerBean>() {
@Override
public void onSuccess(BannerBean bannerBean) {
//设置图片网址或地址的集合
banner.setImages(bannerBean.getData());
//设置轮播图的标题集合
banner.setBannerTitles(getTitles(bannerBean));
//设置是否为自动轮播,默认是“是”
banner.isAutoPlay(true);
//设置轮播间隔时间
banner.setDelayTime(3000);
//设置指示器的位置,小点点,左中右。
banner.setIndicatorGravity(BannerConfig.RIGHT);
//启动轮播图
banner.start();
}
//得到轮播图的表题方法
private List<String> getTitles(BannerBean bannerBean) {
//先初始化集合 再得到集合 里的数据
List<String> list = new ArrayList<>();
//循环
for (BannerBean.DataBean bean : bannerBean.getData()){
list.add(bean.getTitle());
}
//返回值是list 集合
return list;
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dp"
android:textColor="@color/colorPrimary"
android:text="这里是第二层页面 展示Banner 轮播"
/>
<com.youth.banner.Banner
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/MyBanner"
></com.youth.banner.Banner>
</LinearLayout>
implementation 'com.youth.banner:banner:1.4.10'