效果图

所需依赖
依赖较多
implementation ‘com.jakewharton:butterknife:10.2.1’//Butterknife配置
annotationProcessor ‘com.jakewharton:butterknife-compiler:10.2.1’//Butterknife注解处理器
implementation ‘com.github.bumptech.glide:glide:4.10.0’//图片加载框架
implementation 'androidx.recyclerview:recyclerview:1.1.0'//recycerview
implementation 'org.greenrobot:eventbus:3.1.1' //eventbus 事件总线,传值
//retrofit核心库
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
//retrofit辅助,gson解析的库
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.2' // 必要依赖,和Rxjava结合必须用到,下面会提到
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'//切换到主线程的依赖
//okhttp日志拦截器
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.2'
首先先创建左右侧的Entity
创建ApiService接口
public interface ApiService {
@GET(“左侧尾部地址”)
Observable < LeftEntity > getResult();
//Observable导io包,左侧Entity的名称及获取的集合名称
@GET(“右侧尾部地址”)
Observable< RightEntity > getRight(@QueryMap HashMap<String,String> params);
//右侧Entity的名称及获取的集合名称(获取map,创建Hasmap)
}
创建RetrofitUtils工具类
public class RetroitUtils {
private static RetroitUtils retroit;
private final Retrofit retrofit;
public RetroitUtils() {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
retrofit = new Retrofit.Builder()
.baseUrl("左右侧前段相同地址")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
public static RetroitUtils getInstance() {
if (retroit==null){
synchronized (RetroitUtils.class){
if (retroit==null){
retroit=new RetroitUtils();
}

本文档详细介绍了如何在Android应用中使用Retrofit库实现左右联动的效果。内容包括依赖配置、实体类创建、ApiService接口定义、Retrofit工具类、MVP模式的Model、View、Presenter层的实现,以及左右侧适配器的配置。适用于Android初学者学习。
2023

被折叠的 条评论
为什么被折叠?



