Android使用EventBus,RXJava+Retrofit,Butterknife框架开发

为了规范一点写代码,我还是把项目的包设计成比较通用的模式,在校期间靠课程表实在是学不到什么,后来网上学习了这样的写法,引用了。。。

我参考了多篇文章,当时是在别人电脑写好的word文档,我直接复制到这里来了,所以参考链接什么的,都忘记了,要是涉及到版权问题?我再删。总之,这篇文章是建立在前辈们的文章基础上的。

目录结构图:

Android使用EventBus,RXJava+Retrofit,Butterknife框架开发_第1张图片
目录结构

1、activity即 UI;

2、[endif]Adapter主要是Listview或者RecyclerView的数据适配器

3、[endif]api,接口的实现。

4、[endif]base,封装BaseActivity和BaseApplication方便activity的使用。

5、[endif]common,常亮静态类

6、[endif]Database,LitePal所用到的本地数据的包

7、[endif]EventBus,EventBus所需要用到的消息返回,需要把消息封装起来。

8、[endif]utils,工具类。如自定义Logger。

配置

粘贴如下引用,基本上可以实现多数功能,在build.gradle(app)中添加以下代码

    implementation 'org.greenrobot:eventbus:3.1.1'

    implementation 'com.jakewharton:butterknife:8.8.1'

    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

    implementation 'io.reactivex.rxjava2:rxjava:2.1.3'

    implementation 'com.squareup.retrofit2:retrofit:2.3.0'

    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'

    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'

    implementation 'com.squareup.okhttp3:okhttp:3.9.0'

    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'

   implementation 'com.github.xxl6097:okhttputils:2.4.1'

    implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'

    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'

    implementation 'org.litepal.android:core:2.0.0'

    implementation 'com.android.support:recyclerview-v7:27.1.1'

    implementation 'com.alibaba:fastjson:1.2.31'

添加完之后记得同步一下,如果有报错就根据提示修改即可,我的as需要改动android.support版本为27.1.1,build.gradle(project)需要添加以下代码:

allprojects {

    repositories {

        maven { url "https://jitpack.io" }

    }

}

一、RxJava+retrofit网络异步加载框架。

通过观察者的模式观察被观察者--------太过抽象

这是在activity中的代码


Android使用EventBus,RXJava+Retrofit,Butterknife框架开发_第2张图片
activity调用方法

Service是自己创建的类,实例化后调用类内的get****()方法获取数据,.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())基本上是固定形式,subscribe(new Observer(Class)),这个class是查看接口返回的json数据格式而定义的类,注意类的字段名一定要和json的key一致,否则获取不到数据的。截下来看看service类的实现方法。

Service类的实例化实现依赖于Retrofit

框架,先看包的内容如图

Android使用EventBus,RXJava+Retrofit,Butterknife框架开发_第3张图片

可以看到retrofit的工具类和service类在

同一个包里,先看APIService的代码,非

常简单,内部写自己所需要的方法,注

意方法参数。

WeaResponse则是根据api接口返回的json数据所封装的类。

service接口


WeaApiRetrofit类代码,具体的功能已有部分注释,总之可以理解为它的作用是:以ip连接的材料,去实例化service类。


Android使用EventBus,RXJava+Retrofit,Butterknife框架开发_第4张图片

然后再activity中是这样实例化service接口的

service接口的实例化


最后在activity中调用自定义的方法getWeaDatas时,里面的OnNext()方法中即可将数据取出来。只是List<>数据的取值,比较简单,这里不做说明。


二、LitePal本地数据库加载框架

1、配置litepal.xml文件,文件内容格式如下:

    

    

    

        

    

    

文件的位置:


Android使用EventBus,RXJava+Retrofit,Butterknife框架开发_第5张图片
litepal.xml的位置

Tip:list标签处是数据表,注意每一张表都必须对应一个javabean类,想要id是唯一值,设置bean类中在id的声明处写,类必须继承LitePalSupport

@Column(unique = true, defaultValue = "unknown")

Int id;

如图


Android使用EventBus,RXJava+Retrofit,Butterknife框架开发_第6张图片

2、[endif]需要在程序运行的时候,初始化一下LitePal框架。那么我这里因为写好了 baseApplication,即可直接在

该类中实例化,如图:


实例化LitePal


3、[endif]截下来就可以直接使用了。

//增

user=new User(2,"张三","1598765987");

boolean result=user.save();

//删

int result= LitePal.delete(User.class,1);

//查

LitePal.findAllAsync(User.class).listen(new FindMultiCallback() {}.....

//改......

这些方法都是比较多的,初学者可以一个个慢慢找,我就不再说明了,我主要介绍它的配置到使用,增删查改的方法有很多,以后可以慢慢研究。

三、EventBus实例化控件,绑定事件等框架

这个框架我主要使用来传输数据,activity之间。

如图,点击之后跳转到另一个activity,然后输入姓名,


Android使用EventBus,RXJava+Retrofit,Butterknife框架开发_第7张图片

点击确定后返回到这个界面,然后这个文本显示刚刚

输入的内容。

Activity1以下称为A,Activity2以下称为B。

在A里面:

如果要使用这个框架,那么就要注册它

//注册EventBus

EventBus.getDefault().register(this);

然后设置它返回时的方法

@Subscribe(threadMode = ThreadMode.MAIN)

public void setNameByResult(MessageEventBus1 message){

Logger.i("得到的返回数据内容:"+message.toString());

}

MessageEventBus1 这个类是自定义的类,用于封装另一个activity输入的数据。该方法的名字可以随便取,但是要在头部加一行代码,如图。

跳转到B后,B的代码:

在你的按钮的点击事件下输入以下代码:

MessageEventBus1 messageEventBus1=new MessageEventBus1(Constant.InputType.Name,name);

EventBus.getDefault().post(messageEventBus1);

Logger.i("返回的数据"+messageEventBus1.toString());

finish();

返回到A之后,即可看到输入的数据。


未完待续......

你可能感兴趣的:(Android使用EventBus,RXJava+Retrofit,Butterknife框架开发)