本文主要對咱們項目中的使用過的一些庫流行庫作些介紹,後期再對其中的一些庫,如RxJava,RxAndroid,retrofit ,androidannotations,react-native,作細節的分析,到時候再附上使用的demo。html
本文中提到的庫,都是目前流行的,並且使用量比較大,是很是好用的庫,強烈推薦~java
觀察者模式、響應式編程、函數式風格、簡化代碼,更輕鬆的使用併發,開發必備神器~~~react
github源碼android
rxjava相關git
android studio中引入,build.grade的dependencies中引用舉例:github
1 dependencies { 2 compile 'io.reactivex:rxjava:1.0.y-SNAPSHOT' 3 }
簡單示例:web
1 Observable.create(new Observable.OnSubscribe<ArrayList<MyItem>>() { 2 3 @Override 4 public void call(Subscriber<? super ArrayList<MyItem>> subscriber) { 5 //通常爲耗時操做,網絡獲取數據或者讀取數據庫等 6 ArrayList<MyItem> localData = MyDbManager.getDbDatas(); 7 subscriber.onNext(localData); //數據獲取以後,返回獲取的數據 8 subscriber.onCompleted(); 9 } 10 }) 11 .subscribeOn(Schedulers.io()) //獲取數據在io線程中 12 .observeOn(AndroidSchedulers.mainThread()) //獲得數據以後,在主線程更新界面和數據 13 .subscribe(new Observer<ArrayList<MyItem>>() { 14 @Override 15 public void onCompleted() { 16 17 } 18 19 @Override 20 public void onError(Throwable e) { 21 22 } 23 24 @Override 25 public void onNext(ArrayList<MyItem> items) { 26 //獲得數據,do something 27 } 28 });
RxBinding:數據庫
1 //防止多擊,500ms內算一次點擊 2 RxView.clicks(view) 3 .throttleFirst(500, TimeUnit.MILLISECONDS) 4 .subscribe(new Action1<Void>() { 5 @Override 6 public void call(Void aVoid) { 7 //點擊事件處理 8 } 9 });
RxLifecycle:編程
1 //僞代碼 2 Observable.compose(this.<MyData>bindToLifecycle()) //activity中 3 Observable..compose(this.<MyData>bindUntilEvent(FragmentEvent.DETACH)) //Fragment中
網絡請求比較流行的幾個開源庫,咱們項目中基本都用上了,此處作一些簡單介紹。我的最喜歡retrofit,結合Rxjava,RxAndroid簡直完美~windows
1 git clone https://android.googlesource.com/platform/frameworks/volley
1 Picasso.with(context).load(uri).placeholder(R.drawable.placeholder).into(view);
1 Glide.with(context).load(uri).placeholder(R.drawable.placeholder).into(view);
1 dependencies { 2 debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1' 3 forTestCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1' 4 releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1' 5 }
簡單示例:
1 public class MyApplication extends MultiDexApplication { 2 3 private RefWatcher mRefWatcher; 4 5 @Override 6 public void onCreate() { 7 super.onCreate(); 8 // init memory leak detection 9 mRefWatcher = LeakCanary.install(this); 10 } 11 12 public static RefWatcher getRefWatcher(Context context) { 13 MyApplication application = (MyApplication) context.getApplicationContext(); 14 return application.mRefWatcher; 15 } 16 } 17 18 //監控你想要監控的對象。以此爲例: 19 public class BaseFragment extends RxFragment { 20 @Override 21 public void onDestroy() { 22 super.onDestroy(); 23 if (getActivity() != null) { 24 RefWatcher refWatcher = ZYApplication.getRefWatcher(getActivity()); 25 refWatcher.watch(this); 26 } 27 } 28 }
簡單示例:
1 public class AccountEvent { 2 private User user;//你想要傳遞的數據 3 public AccountEvent(User user) { 4 this.user = user; 5 } 6 7 public User getUser() { 8 return user; 9 } 10 11 public void setUser(User user) { 12 this.user = user; 13 } 14 } 15 16 public class LoginActivity { 17 public loginSuccess(User user) { 18 EventBus.getDefault().post(new AccountEvent(user));//發消息 19 } 20 } 21 22 public class MyFragment{ 23 @Override 24 public void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 EventBus.getDefault().register(this); 27 } 28 29 @Override 30 public void onDestroy() { 31 super.onDestroy(); 32 EventBus.getDefault().unregister(this); 33 } 34 35 public void onEvent(AccountEvent event) {//接受消息 36 //do something 37 } 38 }
簡單示例
1 @EActivity(R.layout.activity_my) 2 public class MyActivity extends BaseActivity { 3 @StringRes(R.string.my_string) 4 String mMyString; 5 6 @ViewById(R.id.tv) 7 TextView mTV; 8 9 @Extra() 10 int mCount; 11 12 @Pref 13 UserPreference_ mUserPreference; 14 15 @AfterViews 16 void initialize() { 17 //初始化數據 18 } 19 20 @Click(R.id.finish_iv) 21 void finish() { 22 //do something 23 } 24 25 public void loginSuccess(){ 26 mUserPreference.edit().hasLogin().put(true).apply(); 27 } 28 } 29 30 @SharedPref(value = SharedPref.Scope.UNIQUE) //做用域:整個應用均可以使用 31 public interface UserPreference { 32 @DefaultBoolean(false) 33 boolean hasLogin(); 34 }
來自:http://www.jianshu.com/p/6db1a5e84d67