依赖
//sql
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
//三方
maven { url "https://jitpack.io" }
///
apply plugin: 'org.greenrobot.greendao' // apply plugin
dependencies下依赖
//butterknife
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//okhttp/拦截器
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
//Gson解析
implementation 'com.google.code.gson:gson:2.8.1'
//Glide图片加载
implementation 'com.github.bumptech.glide:glide:4.8.0'
//recyclerView
implementation 'com.android.support:recyclerview-v7:28.0.0'
//MetrialDesign:Design库
implementation 'com.android.support:design:28.+'
//Brvah:RecyclerView快速开发框架
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
//fresco
implementation 'com.facebook.fresco:fresco:1.13.0'
//魅族banner
implementation 'com.github.pinguo-zhouwei:MZBannerView:v2.0.2'
//SmartRefreshLayout
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-19'
//sql
implementation 'org.greenrobot:greendao:3.2.2' // add library
//xRecycleView
implementation 'com.jcodecraeer:xrecyclerview:1.5.9'
//XBanner
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.github.xiaohaibin:XBanner:1.6.1'
//retrofit
implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
//rxjava
implementation 'io.reactivex.rxjava2:rxjava:2.0.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
//eventbus
implementation 'org.greenrobot:eventbus:3.0.0'
greendao {
schemaVersion 1 //数据库版本号
daoPackage 'gxq.com.rk02.greenDao.db'
// 设置DaoMaster、DaoSession、Dao 包名
targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录
generateTests false //设置为true以自动生成单元测试。
targetGenDirTests 'src/main/java' //应存储生成的单元测试的基本目录。默认为 src / androidTest / java。
}
//eventbus 简单的发送接收
public class MessageEvent {
private String message;
public MessageEvent(String message){
this.message=message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
//发送
public class ShowActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
button = findViewById(R.id.show_but);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new MessageEvent("12345上山打老虎老虎没打到打到小松鼠松鼠有几只12345"));
finish();
}
});
}
}
//接收
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.main_but);
//注册
EventBus.getDefault().register(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ShowActivity.class);
startActivity(intent);
}
});
}
//接收
//主线程通信
@Subscribe(threadMode = ThreadMode.MAIN)
public void txtData(MessageEvent messageEvent){
button.setText(messageEvent.getMessage());
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
//retrofit+Rxjava
//Api
public interface Api {
//登录
@FormUrlEncoded
@POST("small/user/v1/login")
Observable postLoginData(@Field("phone") String name, @Field("pwd") String pass);
//注册
@FormUrlEncoded
@POST("small/user/v1/register")
Observable postRegisterData(@Field("phone") String name, @Field("pwd") String pass);
//banner
@GET("small/commodity/v1/bannerShow")
Observable getBannerData();
//首页商品:http://172.17.8.100/small/commodity/v1/commodityList
@GET("small/commodity/v1/commodityList")
Observable getHome_ShopData();
//点击详情 http://172.17.8.100/small/commodity/v1/findCommodityDetailsById
//@Header("userId") int userId,@Header("sessionId") String sessionId
@GET("small/commodity/v1/findCommodityDetailsById")
Observable getXiangQingData(@Query("commodityId") int id);
}
//数据请求单例
public class RetrofitUtils {
public static Api api;
private RetrofitUtils(){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
//.addInterceptor(new )
.readTimeout(5, TimeUnit.SECONDS)
.connectTimeout(5, TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(Constant.WAI_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
api = retrofit.create(Api.class);
}
public static RetrofitUtils getInstens(){
return HttpInstens.retrofitUtils;
}
private static class HttpInstens{
public static RetrofitUtils retrofitUtils = new RetrofitUtils();
}
}
//pceng
public class HomePagePresenter extends BasePresenter {
private final RetrofitUtils instens;
public HomePagePresenter(){
instens = RetrofitUtils.getInstens();
}
public void loadBannerData(){
instens.api.getBannerData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(BannerBean loginBean) throws Exception {
getView().onBannerSuccess(loginBean);
}
});
}
public void laodShopData(){
instens.api.getHome_ShopData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(Home_ShopBean loginBean) throws Exception {
getView().onShopSuccess(loginBean);
}
});
}
}
//App
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
//设置磁盘缓存
//设置大小
DiskCacheConfig cacheConfig = DiskCacheConfig.newBuilder(this)
.setBaseDirectoryName("fimages")
.setMaxCacheSize(30*ByteConstants.MB)
.setBaseDirectoryPath(Environment.getExternalStorageDirectory())
.build();
//设置磁盘缓存的配置,生成配置文件
ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
.setMainDiskCacheConfig(cacheConfig)
.build();
//Fresco
Fresco.initialize(this,config);
//数据库
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "ccc.db");
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
private static DaoSession daoSession;
public static DaoSession getDaoSession() {
return daoSession;
}
}
//utils
//ok
public class OkHttpUtils {
private final OkHttpClient okHttpClient;
//私有化
private OkHttpUtils(){
okHttpClient = new OkHttpClient.Builder().build();
}
public static OkHttpUtils getHttpUtilsInstance(){
return HttpUtilsInstance.okHttpUtils;
}
private static class HttpUtilsInstance{
public static OkHttpUtils okHttpUtils = new OkHttpUtils();
}
//get
public void getData(String url, final Class tClass , final CallBackData callBackData){
this.backData = callBackData;
final Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callBackData.onFail(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Gson gson = new Gson();
T t = gson.fromJson(string, tClass);
callBackData.onResponse(t);
}
});
}
//post
public void postData(String url, HashMap hashMap, final Class tClass, final CallBackData callBackData){
this.backData = callBackData;
FormBody.Builder builder = new FormBody.Builder();
for (String key : hashMap.keySet()) {
builder.add(key,hashMap.get(key));
}
FormBody formBody = builder.build();
final Request request = new Request.Builder().url(url).post(formBody).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callBackData.onFail(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Gson gson = new Gson();
T t = gson.fromJson(string, tClass);
callBackData.onResponse(t);
}
});
}
//接口回调
CallBackData backData;
//接口回调
public interface CallBackData{
void onResponse(D d);
void onFail(String massage);
}
}
//判断网络
//判断网络
public boolean isNetWirkConnected(Context context){
if (context!=null){
ConnectivityManager systemService = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = systemService.getActiveNetworkInfo();
if (activeNetwork!=null){
return activeNetwork.isAvailable();
}
}
return false;
}
//Xbanner
banner.setData(result,null);
banner.loadImage(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
BannerBean.ResultBean model1 = (BannerBean.ResultBean) model;
Glide.with(getActivity()).load(model1.getImageUrl()).into((ImageView) view);
}
});
// 3d MZBanner
// 设置数据
banner.setPages(result, new MZHolderCreator() {
@Override
public BannerViewHolder createViewHolder() {
return new BannerViewHolder();
}
});
//BannerViewHolder
public static class BannerViewHolder implements MZViewHolder {
private ImageView mImageView;
@Override
public View createView(Context context) {
// 返回页面布局
View view = LayoutInflater.from(context).inflate(R.layout.item_banner, null);
mImageView = (ImageView) view.findViewById(R.id.banner_image);
return view;
}
@Override
public void onBind(Context context, int position, BannerBean.ResultBean data) {
// 数据绑定
Glide.with(context).load(data.getImageUrl()).into(mImageView);
}
}