下面分析基于MeiZhi Version 2.6.2
须知少时凌云志,曾许人间第一流
LiteOrm NumberPRogressBar HeadsUp smooth-app-bar-layout
干货集中营 API 文档 GankApi BaseUrl http://gank.io/api/ 1. http://gank.io/api/data/福利/” + DrakeetFactory.meizhiSize + “/{page} ex: http://gank.io/api/data/福利/10/1 2. http://gank.io/api/day/{year}/{month}/{day} ex: http://gank.io/api/day/2017/1/22 3. http://gank.io/api/data/休息视频/” + DrakeetFactory.meizhiSize + “/{page} ex: http://gank.io/api/data/休息视频/10/1
DrakeetApi 1.BaseUrl https://leancloud.cn:443/1.1/classes/ ex https://leancloud.cn:443/1.1/classes/Gank?limit=1&where={“tag”:”2017-1-22”} 其中头信息: X-LC-Id: 0azfScvBLCC9tAGRAwIhcC40 X-LC-Key: gAuE93qAusvP8gk1VW8DtOUb Content-Type: application/json
可以用Fiddler调试
Gson使用中遇到的Date格式问题 serializeNulls Android中Gson解析json数据使用@SerializedName注解与java对象不匹配的字段
App继承自Application,在App中完成了Toasts工具类的注册,及数据库的的创建(LiteOrm),App的主题继承自Theme.AppCompat.Light.NoActionBar
,没有ActionBar,以便自定义。
BaseActivity主要封装了网络请求sGankIO,以及在activity onDestroy
时取消当前网络的请求。 CompositeSubscription的作用:
为了防止可能的内存泄露,在你的 Activity 或 Fragment 的 onDestroy 里,用 Subscription.isUnsubscribed() 检查你的 Subscription 是否是 unsubscribed。如果调用了 Subscription.unsubscribe() ,Unsubscribing将会对 items 停止通知给你的 Subscriber,并允许垃圾回收机制释放对象,防止任何 RxJava 造成内存泄露。
给Android开发者的RxJava详解
我所理解的RxJava——上手其实很简单 [译] 网络请求框架 Retrofit 2 使用入门 DrakeetFactory 第一步-定义请求的Api
public interface GankApi { @GET("data/福利/" + DrakeetFactory.meizhiSize + "/{page}") Observable<MeizhiData> getMeizhiData(@Path("page") int page); @GET("day/{year}/{month}/{day}") Observable<GankData> getGankData( @Path("year") int year, @Path("month") int month, @Path("day") int day); @GET("data/休息视频/" + DrakeetFactory.meizhiSize + "/{page}") Observable<休息视频Data> get休息视频Data(@Path("page") int page);}第二步-Retrofit客户端
public class DrakeetRetrofit { final GankApi gankService; final DrakeetApi drakeetService; // @formatter:off final static Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") .serializeNulls() .create(); // @formatter:on DrakeetRetrofit() { OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); if (DrakeetFactory.isDebug) { //开启log显示到控制台 HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); httpClient.addInterceptor(logging); } httpClient.connectTimeout(12, TimeUnit.SECONDS); OkHttpClient client = httpClient.build(); Retrofit.Builder builder = new Retrofit.Builder(); builder.baseUrl("http://gank.io/api/") .client(client) .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())) .addConverterFactory(GsonConverterFactory.create(gson)); Retrofit gankRest = builder.build(); builder.baseUrl("https://leancloud.cn:443/1.1/classes/"); Retrofit drakeetRest = builder.build(); gankService = gankRest.create(GankApi.class); drakeetService = drakeetRest.create(DrakeetApi.class); } public GankApi getGankService() { return gankService; } public DrakeetApi getDrakeetService() { return drakeetService; }}第三部-生成单例GankApi实例
public class DrakeetFactory { protected static final Object monitor = new Object(); static GankApi sGankIOSingleton = null; static DrakeetApi sDrakeetSingleton = null; public static final int meizhiSize = 10; public static final int gankSize = 5; public static boolean isDebug = true; public static GankApi getGankIOSingleton() { synchronized (monitor) { if (sGankIOSingleton == null) { sGankIOSingleton = new DrakeetRetrofit().getGankService(); } return sGankIOSingleton; } } public static DrakeetApi getDrakeetSingleton() { synchronized (monitor) { if (sDrakeetSingleton == null) { sDrakeetSingleton = new DrakeetRetrofit().getDrakeetService(); } return sDrakeetSingleton; } }}ToolbarActivity
继承自BaseActivity
ToolbarActivity
主要设置了当前Activity
的actionbar
,可以给子类覆盖重写是否可以显示返回上一个Activity
按钮,有显示隐藏AppBarLayout方法。
继承自ToolbarActivity的子类须include
的layout
android:theme和app:popupTheme的作用,以及在android 3.0以下不起作用问题的解决 调用
setSupportActionBar(mToolbar);判断是否有返回箭头,ToolbarActivity默认是不显示返回按钮(即返回上一个Activity),需要子类覆盖重写。
ActionBar actionBar = getSupportActionBar(); //显示返回的箭头,并可通过onOptionsItemSelected()进行监听,其资源ID为android.R.id.home。 if (actionBar != null) actionBar.setDisplayHomeAsUpEnabled(true);返回按钮的点击事件
@Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { onBackPressed(); return true; } else { return super.onOptionsItemSelected(item); } }显示或隐藏ToolBar
protected void hideOrShowToolbar() { mAppBar.animate() .translationY(mIsHidden ? 0 : -mAppBar.getHeight()) .setInterpolator(new DecelerateInterpolator(2)) .start(); mIsHidden = !mIsHidden; }SwipeRefreshBaseActivity
继承自ToolbarActivity
封装了SwipeRefreshLayout
的请求状态和配置了请求的UI效果。
设置旋转箭头的颜色变化
mSwipeRefreshLayout.setColorSchemeResources(R.color.refresh_progress_3, R.color.refresh_progress_2, R.color.refresh_progress_1);MainActivity
继承自SwipeRefreshBaseActivity
浮动操作按钮详解
待续….
新闻热点
疑难解答