标签:
本文主要对我们项目中的使用过的一些库流行库做些介绍,后期再对其中的一些库,如RxJava,RxAndroid,retrofit ,androidannotations,react-native,做细节的分析,到时候再附上使用的demo。
本文中提到的库,都是目前流行的,而且使用量比较大,是非常好用的库,强烈推荐~
观察者模式、响应式编程、函数式风格、简化代码,更轻松的使用并发,开发必备神器~~~
android studio中引入,build.grade的dependencies中引用举例:
1 dependencies { 2 compile ‘io.reactivex:rxjava:1.0.y-SNAPSHOT‘ 3 }
简单示例:
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简直完美~
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
标签:
原文地址:http://www.cnblogs.com/huolongluo/p/5929500.html