1.添加网络权限和导入相关的依赖
1).网络权限
java
<uses-permission android:name="android.permission.INTERNET"/>
2).依赖react
//banner依赖 implementation 'com.youth.banner:banner:1.4.10' // 最新版本 implementation 'com.github.bumptech.glide:glide:4.8.0'//glide图片加载框架
2.建立封装类(比较简单,这里就不贴代码了)
3.建立一个接口(ApiService)(网络请求我使用的是RxJava+Retrofit,因此我这里讲的方法是RxJava+Retrofit的,固然网络请求大家也能够用其余的,我在这里只讲这一种方法)
android
String path = "https://www.wanandroid.com/"; @GET("banner/json") Observable<BannerBean> getBanner();
RxJava+Retrofit依赖:git
//RxJava和Retrofit依赖 implementation 'com.squareup.retrofit2:retrofit:2.4.0' implementation 'com.squareup.retrofit2:converter-gson:2.4.0' // 必要依赖,解析json字符所用 implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' // 必要依赖,和Rxjava结合必须用到,下面会提到 implementation "io.reactivex.rxjava2:rxjava:2.1.3" // 必要rxjava2依赖 implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
4.适配器(我弄的多布局样式的适配器,但适配器目前只写了banner)github
private Context context; private List<BannerBean.DataBean> data = new ArrayList<>(); public static final int BANNER_TYPE = 0; public BannerAdapter(Context context) { this.context = context; } public void setData(List<BannerBean.DataBean> data) { this.data.addAll(data); notifyDataSetChanged(); } @Override public int getItemViewType(int position) { if(position==0){ return BANNER_TYPE; } return 0; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if(viewType==BANNER_TYPE){ View inflate = LayoutInflater.from(context).inflate(R.layout.banner_item, parent, false); return new BannerViewHolder(inflate); } return null; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { int itemViewType = getItemViewType(position); if(itemViewType==0){ BannerViewHolder bannerViewHolder = (BannerViewHolder) holder; bannerViewHolder.banner.setImages(data).setImageLoader(new ImageLoader() { @Override public void displayImage(Context context, Object path, ImageView imageView) { BannerBean.DataBean bean = (BannerBean.DataBean) path; Glide.with(context).load(bean.getImagePath()).into(imageView); } }).start(); } } @Override public int getItemCount() { return data !=null ? 1:0; } private class BannerViewHolder extends RecyclerView.ViewHolder { Banner banner; public BannerViewHolder(View inflate) { super(inflate); banner = inflate.findViewById(R.id.banner); } }
5.banner_item的布局:json
<com.youth.banner.Banner android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/banner" />
6.activity:布局是RecyclerView,这里就不贴代码了api
private RecyclerView recycler; private BannerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { Retrofit build = new Retrofit.Builder() .baseUrl(ApiService.path) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); final ApiService apiService = build.create(ApiService.class); apiService.getBanner().subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<BannerBean>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(BannerBean bannerBean) { List<BannerBean.DataBean> data = bannerBean.getData(); adapter.setData(data); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); } private void initView() { recycler = findViewById(R.id.recycler); recycler.setLayoutManager(new LinearLayoutManager(this)); adapter = new BannerAdapter(this); recycler.setAdapter(adapter); }
好了,到这里就大功告成了,欢迎你们提出意见!网络
下面给你们找了一些banner的其余属性,具体其余的若是下面没有,那就自行百度吧!
框架