?? Rxjava the simple use of basically also understand, in fact, there is a more fun is the java8 only lambda.
Lambda under Android Studio environment build download JAVA8
?? Below to build this environment, because Android does not support JAVA8, so need to use an open source library, RETOLAMBDA, click here. Specifically how to use basically have, here simply introduced, the first is to download java8: Download Java8, click here.
Modify a configuration project file
?? After the installation is ready, you need to modify the 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:1.5.0‘ classpath ‘me.tatarka:gradle-retrolambda:3.2.0‘ // 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}
?? The me.tatarka:gradle-retrolambda:3.2.0 is added here.
?? Next is the Build.gradle in the app directory:
apply plugin: ‘com.android.application‘apply plugin: ‘me.tatarka.retrolambda‘android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.jared.emrxandroidstudy" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }}dependencies { compile fileTree(dir: ‘libs‘, include: [‘*.jar‘]) testCompile ‘junit:junit:4.12‘ compile ‘com.android.support:appcompat-v7:23.1.1‘ compile ‘io.reactivex:rxandroid:1.1.0‘ compile ‘io.reactivex:rxjava:1.1.0‘}
Modify the JDK version of the project
?? You need to modify the compiled JDK to Java8 after adding:
Lambda is simple to use in rxandroid
?? After the modification, restart the project, and then we begin to continue based on the previous article. The code for the previous article is simplified by lambda:
Private void Createobservablebymap() {LOG.D (TAG,"Createobservablebymap"); Observable.just (Gethello ()). Map (NewFunc1<string, string> () {@Override PublicStringPager(String s) {returnS +"by Eastmoon"; }}). Subscribe (onnextaction); }onnextaction =NewAction1<string> () {@Override Public void Pager(String s) {Mhello.settext (s); } };
?? This is simplified as follows:
privatevoidcreateObservableBylambda() { "createObservableBylambda"); Observable.just(getHello()) " by eastmoon") .subscribe(s -> mHello.setText(s)); }
?? is not very clear, very concise and elegant, here first not analysis.
Introduction to lambda Expressions Lambda Brief introduction and examples
?? A simple understanding of lambda, lambda is an anonymous expression, about lambda expressions This article is good: talk about lambda better article. This is still recorded as a study, first lambda expression is generally:
(argument) -> (body)
?? Where argument represents the parameter, body indicates what the function body is going to do. The commonly used expressions are as follows:
(arg1, arg2...) -> { body }(type1 arg1, type2 arg2...) -> { body }
?? Specifically, this is the case:
(intint b) -> { return a + b; } () -> System.out.println("Hello World"42a 5return3.1415 };
Functional Interface Simple Introduction
?? Through the above example, we basically understand how the expression is used. So what can you do with lambda expressions? Here is the concept of a functional interface, which refers to an interface that contains only an abstract method declaration. Runnable has only one interface run, so it can be implemented with a lambda expression. The normal way is as follows:
new Runnable() { @Override publicvoidrun() { "testlambda"); } };
?? Use the lambda expression as follows:
"testlambda");
?? From six lines of code to get a line of code, is not super simple and elegant. Let's try the water for an example:
privatevoidtestlambda() { new Thread(new Runnable() { @Override publicvoidrun() { "test by normal func"); } }).start(); new"test by lambda func")).start(); }
?? Here you can see that the two effects are identical by creating a thread to output information in the Run method:
03-1109:18:39.45327601-28067/? D/MainActivity: test by lambda func03-1109:18:39.45327601-28066/? D/MainActivity: test by normal func
Simple implementation with functional interface
?? Since a functional interface is required, we will simply implement the following function interface and create a new FUNCTIONLAMBDA interface:
package com.jared.emrxandroidstudy;/** * Created by jared on 16/3/11. */publicinterface FunctionLambda { publicvoidhello();}
?? Then write the class Functionlambdatest:
package com.jared.emrxandroidstudy;/** * Created by jared on 16/3/11. */publicclass FunctionLambdaTest { publicstaticvoidhelloTest(FunctionLambda functionLambda) { functionLambda.hello(); }}
?? Then we will implement this function:
privatevoidtestFunctionLambda() { FunctionLambdaTest.helloTest(new FunctionLambda() { @Override publicvoidhello() { "test by normal testFunctionLambda"); } }); "test by lambda testFunctionLambda")); }
?? The output information is as follows:
03-1109:30:29.00528776-28776/? D/MainActivity: test by normal testFunctionLambda03-1109:30:29.00528776-28776/? D/MainActivity: test by lambda testFunctionLambda
?? The effect is exactly the same, and the code is streamlined.
Analysis of the use of lambda in rxandroid
?? OK, so many lambda expressions, let's look at the example rxandroid used. Add the code here:
privatevoidcreateObservableBylambda() { "createObservableBylambda"); Observable.just(getHello()) " by eastmoon") .subscribe(s -> mHello.setText(s)); }
?? The first is the map method, because the map method overrides the call method, the passed parameter is s, the function body inside to do is s+ "by Eastmoon", so it is written as S-s+ "by Eastmoon", from this is known as s into the S+ "by Eastmoon ". Similarly, the Subscribe method is the same as the incoming Mhello.settext (s) that needs to be processed.
?? Basically the simple use of lambda OK, then continue to learn rxandroid.
Android Development Learning Path--rxandroid Lambda