The path to Android development and learning-lambda of RxAndroid
The simple use of RxJava is basically also known. In fact, there is also a more interesting lambda that is only available in Java 8.
Lambda is built in the android studio environment. Download java8
Next we will build this environment. Because android does not support Java 8, we need to use an Open Source library, retolambda. Click here. You can use Java 8 as follows: Click here to download Java 8.
Modify the configuration project file
After the installation is downloaded, You need to modify 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}
Me. tatarka: gradle-role lambda: 3.2.0 is added here.
Next, 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
After adding the jdk, You need to modify the compiled jdk to java8:
Simple use of lambda in RxAndroid
After the modification, restart the project, and then we start to continue based on the previous article. Lambda simplifies the code in the previous article:
private void createObservableByMap() { Log.d(TAG, "createObservableByMap"); Observable.just(getHello()).map(new Func1
() { @Override public String call(String s) { return s + " by eastmoon"; } }).subscribe(onNextAction); }onNextAction = new Action1
() { @Override public void call(String s) { mHello.setText(s); } };
The simplified method is as follows:
private void createObservableBylambda() { Log.d(TAG, "createObservableBylambda"); Observable.just(getHello()) .map(s -> s + " by eastmoon") .subscribe(s -> mHello.setText(s)); }
Is it very clear and concise and elegant? I will not analyze it here.
Introduction to lambda expressions
Lambda is an anonymous expression. This article is good about lambda expressions: a good article about lambda. Here we should record it as learning. First, the lambda expression is generally:
(argument) -> (body)
Here, argument indicates a parameter, and body indicates what the function body is going to do. The common expressions are as follows:
(arg1, arg2...) -> { body }(type1 arg1, type2 arg2...) -> { body }
The specific steps are as follows:
(int a, int b) -> { return a + b; } () -> System.out.println("Hello World");(String s) -> { System.out.println(s); }() -> 42a -> a + 5() -> { return 3.1415 };
Introduction to function Interfaces
Through the above example, we have basically understood how to use the expression. So under what circumstances can lambda expressions be used? There is a concept of a functional interface. A functional interface refers to an interface that contains only one abstract method declaration. Runnable has only one interface run, so it can be implemented using lambda expressions. The common method is as follows:
Runnable r1 = new Runnable() { @Override public void run() { Log.d(TAG, "testlambda"); } };
The lambda expression is as follows:
Runnable r2 = () -> Log.d(TAG, "testlambda");
The Code is a line from six lines. Is it super concise and elegant. Let's try the following example:
private void testlambda() { new Thread(new Runnable() { @Override public void run() { Log.d(TAG, "test by normal func"); } }).start(); new Thread(() -> Log.d(TAG, "test by lambda func")).start(); }
?? The output information of the Creation thread in the run method shows that the two effects are identical:
03-11 09:18:39.453 27601-28067/? D/MainActivity: test by lambda func03-11 09:18:39.453 27601-28066/? D/MainActivity: test by normal func
Simple implementation of functional interfaces
Since function interfaces are required, we can simply implement the function interfaces and create the FunctionLambda interface:
package com.jared.emrxandroidstudy;/** * Created by jared on 16/3/11. */public interface FunctionLambda { public void hello();}
Then write the FunctionLambdaTest class:
package com.jared.emrxandroidstudy;/** * Created by jared on 16/3/11. */public class FunctionLambdaTest { public static void helloTest(FunctionLambda functionLambda) { functionLambda.hello(); }}
Next we will implement this function:
private void testFunctionLambda() { FunctionLambdaTest.helloTest(new FunctionLambda() { @Override public void hello() { Log.d(TAG, "test by normal testFunctionLambda"); } }); FunctionLambdaTest.helloTest(() -> Log.d(TAG, "test by lambda testFunctionLambda")); }
The output information is as follows:
03-11 09:30:29.005 28776-28776/? D/MainActivity: test by normal testFunctionLambda03-11 09:30:29.005 28776-28776/? D/MainActivity: test by lambda testFunctionLambda
The results are exactly the same, and the code is concise.
Usage Analysis of lambda in RxAndroid
Well, after talking about so many lambda expressions, let's take a look at the example used by rxAndroid. Add the following code:
private void createObservableBylambda() { Log.d(TAG, "createObservableBylambda"); Observable.just(getHello()) .map(s -> s + " by eastmoon") .subscribe(s -> mHello.setText(s)); }
?? The first is the map method. Because the call method is rewritten in the map method, the input parameter is s, and what to do in the function body is s + "by eastmoon ", therefore, it is written as s-> s + "by eastmoon", from which we can see that s is changed to s + "by eastmoon ". Similarly, the subscribe method also applies to the incoming mHello. setText (s) to be processed ).
?? Basically, lambda is easy to use. Next, I will continue to learn about RxAndroid.