这里lambda需要java8的支持,先来配置一下环境
在app包下的那个build.gradle中加入如下代码
compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
看一下build.gradle代码
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.fanyafeng.rxandroid"
minSdkVersion 16
targetSdkVersion 22
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.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'com.squareup.okhttp3:okhttp:3.3.1'
compile 'io.reactivex:rxjava:1.1.6'
compile 'io.reactivex:rxandroid:1.2.0'
compile 'com.facebook.fresco:fresco:0.11.0'
compile 'com.huxq17.xrefreshview:xrefreshview:3.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
}
看下需要加的代码的位置,再来看另一个项目目录下的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 '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
}
这里需要加两个path,这样lambda表达式就可以用了。
因为项目和上文的一样,就把activity的代码重新发一下,其他的就不发了,代码中的注释也很详细,详情可以看代码
package com.fanyafeng.rxandroid.hong9.activity;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.facebook.drawee.view.SimpleDraweeView;
import com.fanyafeng.rxandroid.BaseActivity;
import com.fanyafeng.rxandroid.R;
import com.fanyafeng.rxandroid.adapter.RVAdapter;
import com.fanyafeng.rxandroid.hong9.bean.BannerBean;
import com.fanyafeng.rxandroid.hong9.bean.ProductBean;
import com.fanyafeng.rxandroid.hong9.fragment.ViewPagerFragment;
import com.fanyafeng.rxandroid.hong9.network.Urls;
import com.fanyafeng.rxandroid.hong9.response.GetMainResponse;
import com.fanyafeng.rxandroid.hong9.service.ApiService;
import com.fanyafeng.rxandroid.retrofit.response.TaoBaoGetIpInfoResponse;
import java.util.ArrayList;
import java.util.List;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.Observable;
import rx.Observer;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
public class LambdaRedWineActivity extends BaseActivity {
private final static String imageUri = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwh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m25PhrrhzlZLtFf72MnFxMjPy1n6js7ktqqoveFMX6Lu+79fyAHox8nVsuGdqMeFQe9GNvvGry+8/Pp0RvVxUEkiqCUFsh+IEFvEDiK+InEAWcQOIr4gcQJLHIVyE4gOQA7kK5CuQrkAM2K5CuQrkQBnIRyA5COQJGchHIDkI5ABcityI5CNkEkbEbA5CNgBU3CXFF7NF0pV5NLjJbp8muxyNiqxwlxR6/U0svFVy4o+8jdxMp0vR+6zgyqJY9nC+cX8r7nM2blnu8mlp9H/dMxMiqVFjjL+j7mPFyHP1LPeR2201qtipsrbC2VyZuGNsm5BGyArqY2425WMuhY4R36dp12pZCrqW0V8830ij3un4dGn4ypojsvWT6yfdnJplNdGBVGqCinFN7erO+LLJaGNvUvbe6lF7SX+vhltdqmt+jXJp+jOZMm7V1bX8zaflbFip2qRyahqdWn1Rcoysus5VUw+ax/ou79C+PVGLgQjfddlWrjvlOUHN9VFPZJdl+QA+FhXX5Lzs+SsypLZJfLVH8MfHnqzahtFcimHKJZuCC3iJxFQdwCziJxFe4N3sAWcQOIQG/IAfiA5CbgAHchXIVsG4JGchXIVisALkK5AYjIAXIRsj6iMEhcityIxGARyEkySZXIgkjkVyYWVsAMbHXLdf1Xce6MMmnZ9PR+qZzsNLfvNt+TXM0cuhNdbHk0b+HkSjLq3szMuhKqbjLqv9SiTNXPinjuTXNPkzIZfHtdkNXudOQGyCkM5TU//Z";
private final static String headerUri = "http://vinos.b0.upaiyun.com/avatar/default/0247662e-21bf-4662-89fd-d7ebc0600a4e.jpg";
private RVAdapter rvAdapter;
private RecyclerView rvRedWine;
private List<ProductBean> productBeanList = new ArrayList<>();
private SimpleDraweeView sdvDrawerHead;
private SimpleDraweeView sdvUserHead;
private DrawerLayout layoutDrawer;
private NavigationView layoutNavigationView;
private List<Fragment> fragmentList;
private ViewPager staggerViewpager;
private PagerAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_red_wine);
title = "红酒";
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show());
initView();
initData();
}
private void initView() {
rvRedWine = (RecyclerView) findViewById(R.id.rvRedWine);
layoutDrawer = (DrawerLayout) findViewById(R.id.layoutDrawer);
layoutNavigationView = (NavigationView) findViewById(R.id.layoutNavigationView);
staggerViewpager = (ViewPager) findViewById(R.id.staggerViewpager);
fragmentList = new ArrayList<>();
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), fragmentList);
initMainData();
}
private void initData() {
rvAdapter = new RVAdapter(this, productBeanList);
rvRedWine.setHasFixedSize(true);
rvRedWine.setLayoutManager(new GridLayoutManager(this, 2));
rvRedWine.setAdapter(rvAdapter);
layoutNavigationView.inflateHeaderView(R.layout.layout_drawer_head);
sdvDrawerHead = (SimpleDraweeView) layoutNavigationView.getHeaderView(0).findViewById(R.id.sdvDrawerHead);
sdvDrawerHead.setImageURI(Uri.parse(imageUri));
sdvDrawerHead.setAspectRatio(1.33f);
sdvUserHead = (SimpleDraweeView) layoutNavigationView.getHeaderView(0).findViewById(R.id.sdvUserHead);
sdvUserHead.setImageURI(Uri.parse(headerUri));
sdvUserHead.setAspectRatio(1.0f);
layoutNavigationView.inflateMenu(R.menu.menu_drawer_nav);
onMenuCheck(layoutNavigationView);
}
private void initMainData() {
staggerViewpager.setAdapter(new PagerAdapter(getSupportFragmentManager(), fragmentList));
staggerViewpager.setCurrentItem(0);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Urls.HTTP_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
ApiService apiService = retrofit.create(ApiService.class);
apiService.getMainData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getMainResponse -> {
Log.d("redwine", "请求成功:" + getMainResponse.state);
Log.d("redwine", "请求成功:" + getMainResponse.data.banner.get(0).id + "数组长度:" + getMainResponse.data.banner.size());
Log.d("redwine", "请求成功:" + getMainResponse.data.products.get(0).cn_name);
productBeanList.addAll(getMainResponse.data.products);
rvAdapter.notifyDataSetChanged();
Observable.just(getMainResponse.data.banner)//相当于.next
.observeOn(AndroidSchedulers.mainThread())//观察在主线程,LooperScheduler
.flatMap(Observable::from)//from泛型数组
.subscribe(LambdaRedWineActivity.this::bindViewpager);
}, throwable -> {//必须是抛出异常的方法
Log.d("redwine", "异常:" + throwable.toString());
}, () -> Log.d("redwine", "fragmentList长度:" + fragmentList.size()));
}
private void bindViewpager(BannerBean bannerBean) {
ViewPagerFragment viewPagerFragment = new ViewPagerFragment();
Bundle bundle = new Bundle();
bundle.putString("img", bannerBean.img);
viewPagerFragment.setArguments(bundle);
fragmentList.add(viewPagerFragment);
staggerViewpager.setAdapter(pagerAdapter);
Log.d("lambda", "fragmentList长度:" + fragmentList.size());
}
private void onMenuCheck(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.nav_menu_home:
Toast.makeText(LambdaRedWineActivity.this, "点击第一个", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_menu_categories:
break;
case R.id.nav_menu_feedback:
break;
case R.id.nav_menu_setting:
break;
}
item.setChecked(true);
layoutDrawer.closeDrawers();
return true;
});
}
class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
public PagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
}