这是我的分包
1.首先添加依赖
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' //okhttp3
compile 'com.squareup.retrofit2:retrofit:2.0.1' //retrofit网络请求
compile 'com.squareup.retrofit2:converter-gson:2.0.1'//retrofit2 Gson 和/retrofi网络请求一起使用
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'//rxjava
compile 'io.reactivex:rxandroid:1.1.0'//rxandroid和rxjava一起使用
//recyclerview控件依赖包
compile 'com.android.support:mediarouter-v7:26.1.0'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
//fresco图片加载依赖包
compile 'com.facebook.fresco:fresco:0.12.0'
compile 'com.facebook.fresco:animated-base-support:0.12.0'
2.添加网络请求权限
<uses-permission android:name="android.permission.INTERNET"/>//网络权限
准备工作做完就开始实现了
1.首先定义model层view回调接口方法
public interface IModle {
void getUrl(String url);
}
public interface Iview {
//提供规范 提供ui交互
void getSongs(List<MusicBean.SongListBean> song_list);
}
2.在Modle层实现Medle接口方法的耗时操作
/**
* 创建一个请求类 实现Imodle 的接口
*/
public class MusicModle implements IModle {
//暴露接口
private OnFinish onFinish;
//定义一个接口
public interface OnFinish{
void OnFinishListener(ArrayList<MusicBean.SongListBean> song_list);
}
//添加设置方法
public void setOnFinish(OnFinish finish){
this.onFinish=finish;
}
@Override
public void getUrl(String url) {
getParams(url);
}
public void getParams(String url) {
//构建retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)//添加url
.addConverterFactory(GsonConverterFactory.create())//设置Gson工厂模式
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())//RxJava适配器工厂模式
.client(OkhttpUtil.getOkHttpClient())//添加拦截器
.build();
//通过动态代理得到网络接口对象
APIService apiService = retrofit.create(APIService.class);
//得到Observable被观察者
Observable<MusicBean> homeBean = apiService.getMusicBean();
homeBean.subscribeOn(Schedulers.io())//subscribeOn()主要改变的是订阅的线程,即call()执行的线程;//IO线程做耗时操作
.observeOn(AndroidSchedulers.mainThread())//observeOn()主要改变的是发送的线程,即onNext()执行的线程。//在主线程更新UI
//订阅观察者
.subscribe(new Observer<MusicBean>() {
private List<MusicBean.SongListBean> song_list;
//完成时执行
@Override
public void onCompleted() {
Log.d("MainActivity+++++++++", "onCompleted: ");
}
//异常
@Override
public void onError(Throwable e) {
Log.d("MainActivity", "onError: ");
}
//加载中
@Override
public void onNext(MusicBean musicBean) {
Log.d("MainActivity", "onNext: ");
song_list = musicBean.getSong_list();
for (int i = 0; i < song_list.size(); i++) {
Log.i("xxx", song_list.get(i).getAlbum_title());
}
//通过接口方法拿到数据
onFinish.OnFinishListener((ArrayList<MusicBean.SongListBean>) song_list);
}
});
}
}
在MainActivity实现Iview层的接口里的方法
public class MainActivity extends AppCompatActivity implements Iview {
List<MusicBean.SongListBean> song_list;
private MusicPresenter musicPresenter;
private RecyclerView mRecyclerView;
private LinearLayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化presenter类
musicPresenter = new MusicPresenter(this);
//设置url
musicPresenter.setmusic(API.TING_PATH);
mRecyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);
//创建默认的线性LayoutManager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
//如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
mRecyclerView.setHasFixedSize(true);
}
/**
* 实现接口里的方法
* @param song_list
*/
@Override
public void getSongs(List<MusicBean.SongListBean> song_list) {
MyAdapter myAdapter = new MyAdapter(song_list, MainActivity.this);
mRecyclerView.setAdapter(myAdapter);
}
}
在Presenter层进行数据交互
/**
* Presenter是Modle层和View层进行交互的类相当于桥梁
*/
public class MusicPresenter implements MusicModle.OnFinish {
//定义两个常量,一个Iview,一个modle实体类
private final Iview iview;
private final MusicModle musicModel;
//通过构造方法初始化赋值给本类
public MusicPresenter(Iview iview) {
this.iview = iview;//p层得到View实体传来的实体Acivity
this.musicModel=new MusicModle();//通过强引用方式赋值给当前这个类的musicModel
}
public void setmusic(String url){
//modle接口回调
musicModel.getUrl(url);
//拿到modle层的请求数据
musicModel.setOnFinish(this);
}
@Override
public void OnFinishListener(ArrayList<MusicBean.SongListBean> song_list) {
//Iview层的接口回调
iview.getSongs(song_list);
}
}
动态代理以注解方式进行请求返回一个Observable观察者模式
/**
* 动态代理以注解方式进行请求返回一个Observable观察者模式
*/
public interface APIService {
@GET("v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0")
Observable<MusicBean> getMusicBean();
}
创建拦截器工具类.
/**
* 创建拦截器工具类.
*/
public class OkhttpUtil {
private static OkHttpClient okHttpClient = null;
public synchronized static OkHttpClient getOkHttpClient() {
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(15,TimeUnit.SECONDS)
//添加OkHttp3的拦截器
.addInterceptor(new LoggingInterceptor())
.writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS)
.build();
return okHttpClient;
}
}
初始化Fresco
/**
* 初始化Fresco
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
RecyclerView适配器
/**
* RecyclerView适配器
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<MusicBean.SongListBean> song_list;
private Context mContext;
public MyAdapter(List<MusicBean.SongListBean> song_list, Context mContext) {
this.song_list = song_list;
this.mContext = mContext;
}
//创建新View,被LayoutManager所调用
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.re_item_layout, viewGroup, false);
ViewHolder vh = new ViewHolder(view);
return vh;
}
//将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.draweeView.setImageURI(Uri.parse(song_list.get(position).getPic_big()));
viewHolder.mTextView.setText(song_list.get(position).getAlbum_title());
}
//获取数据的数量
@Override
public int getItemCount() {
return song_list.size();
}
//自定义的ViewHolder,持有每个Item的的所有界面元素
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public SimpleDraweeView draweeView;
public ViewHolder(View view) {
super(view);
mTextView = (TextView) view.findViewById(R.id.item_text);
draweeView = (SimpleDraweeView) view.findViewById(R.id.item_simple);
}
}
}
MainActivity布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bwei.luyuanbo20171106.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</android.support.constraint.ConstraintLayout>
RecyclerView的子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="90dp"
xmlns:fresco="http://schemas.android.com/tools">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/item_simple"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="15dp"
fresco:actualImageScaleType="centerCrop"
fresco:roundAsCircle="true"
fresco:placeholderImage="@drawable/icon_placeholder"
fresco:placeholderImageScaleType="fitCenter"
fresco:roundingBorderColor="#f8bcf3"
fresco:roundingBorderWidth="5dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/id_num"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sss"/>
</LinearLayout>