Android Studio 使用Lambda
生活随笔
收集整理的這篇文章主要介紹了
Android Studio 使用Lambda
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1,昨天在使用RxJava的時候,調用map、filter之類的方法要創建挺多的匿名內部類,所以我們打算試用一下Lambda讓我們的代碼更有閱讀新性,下看一下我們的對比
在使用之前我們代碼是這樣的
Observable.from(folders).flatMap(new Func1<File, Observable<File>>() {@Overridepublic Observable<File> call(File file) {return Observable.from(file.listFiles());}}).filter(new Func1<File, Boolean>() {@Overridepublic Boolean call(File file) {return file.getName().endsWith(".png");}}).map(new Func1<File, Bitmap>() {@Overridepublic Bitmap call(File file) {return getBitmapFromFile(file);}}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Bitmap>() {@Overridepublic void call(Bitmap bitmap) {imageCollectorView.addImage(bitmap);}});使用之后
Observable.from(folders).flatMap((Func1) (folder) -> { Observable.from(file.listFiles()) }).filter((Func1) (file) -> { file.getName().endsWith(".png") }).map((Func1) (file) -> { getBitmapFromFile(file) }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((Action1) (bitmap) -> { imageCollectorView.addImage(bitmap) });有沒有很簡潔,來看一下它在Android studio怎么使用吧,首先這是官網地址:https://github.com/evant/gradle-retrolambda,可以按照它這樣一步步集成
①,JDK至少是1.8的(還是1.6或者1.7的同學去重新裝一下)
②,在工程的build.gradle文件中加入下面這句話:
classpath 'me.tatarka:gradle-retrolambda:3.3.1'再把工程的build.gradle全部貼出來看看
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {repositories {jcenter()}dependencies {classpath 'com.android.tools.build:gradle:2.1.0'classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'classpath 'me.tatarka:gradle-retrolambda:3.3.1' //主要是這一句// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files} }allprojects {repositories {jcenter()} }task clean(type: Delete) {delete rootProject.buildDir }再看看App的Build文件添加下面兩句
apply plugin: 'me.tatarka.retrolambda' compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8} }再貼一下App的Build.gradle全部代碼
apply plugin: 'com.android.application' apply plugin: 'com.neenbedankt.android-apt' apply plugin: 'me.tatarka.retrolambda' android {compileSdkVersion 25buildToolsVersion "25.0.0"defaultConfig {applicationId "com.qianmo.myview2"minSdkVersion 15targetSdkVersion 25versionCode 1versionName "1.0"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8} }dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])testCompile 'junit:junit:4.12'compile 'com.android.support:appcompat-v7:25.0.0'compile 'com.android.support:design:25.+'compile 'com.android.support:recyclerview-v7:25.+'compile 'com.android.support:cardview-v7:25.+'compile 'com.balysv:material-ripple:1.0.2'compile 'com.jakewharton:butterknife:8.2.1'apt 'com.jakewharton:butterknife-compiler:8.2.1'compile 'io.reactivex:rxjava:1.1.0'compile 'io.reactivex:rxandroid:1.1.0'compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'compile 'com.google.code.gson:gson:2.6.2' }這樣我們就可以使用了 ?很簡單有沒有!!!!!
轉載于:https://www.cnblogs.com/wjtaigwh/p/6046030.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Android Studio 使用Lambda的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 博为峰Java技术题 ——JavaSE
- 下一篇: 老司机带你深入浅出 Collection