本文主要是介绍MVP操作RecyclerView多条目SpringView,OKHttp封装请求网络,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
build.gradle导入依赖
apply plugin: 'com.android.application'android {compileSdkVersion 25buildToolsVersion "25.0.1"defaultConfig {applicationId "com.example.lianxiday10_myp_recyclerview"minSdkVersion 18targetSdkVersion 25versionCode 1versionName "1.0"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}
}dependencies {compile fileTree(include: ['*.jar'], dir: 'libs')testCompile 'junit:junit:4.12'compile 'com.android.support:appcompat-v7:25.+'compile 'com.android.support:recyclerview-v7:25.3.1'compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'compile 'com.squareup.okhttp3:okhttp:3.9.0'compile 'com.google.code.gson:gson:2.8.2'compile 'com.liaoinstan.springview:library:1.3.0'
}
activity_main的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><com.liaoinstan.springview.widget.SpringViewandroid:id="@+id/spring_view"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v7.widget.RecyclerViewandroid:id="@+id/recycler_view"android:layout_width="match_parent"android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></com.liaoinstan.springview.widget.SpringView>
</RelativeLayout>
MainActivity 实现 view层的接口,成功的回调方法里 去给适配器设置集合数据
public class MainActivity extends AppCompatActivity implements MutiViewCallBack{private SpringView springView;private RecyclerView recyclerView;private MutiAdapter mutiAdapter;private MutiPresenter mutiPresenter;//mvp 展示recycleview 多条目 上拉下拉 使用okhttp封装请求数据@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取idspringView = (SpringView) findViewById(R.id.spring_view);recyclerView = (RecyclerView) findViewById(R.id.recycler_view);//new适配器,将上下文传过去mutiAdapter = new MutiAdapter(this);//new出来presentermutiPresenter = new MutiPresenter(this);//设置布局类型 false是正序LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false);//设置布局管理器recyclerView.setLayoutManager(manager);recyclerView.setAdapter(mutiAdapter);//springview的设置springView.setHeader(new DefaultHeader(this));springView.setFooter(new DefaultFooter(this));springView.setListener(new SpringView.OnFreshListener() {@Overridepublic void onRefresh() {//下拉刷新mutiPresenter.onRefresh(true);}@Overridepublic void onLoadmore() {//上拉加载mutiPresenter.onRefresh(false);}});//进入页面先访问数据mutiPresenter.onRefresh(true);}//实现MutiViewCallBack@Overridepublic void success(MutiBean mutibean) {//这个回调是mutiview的 presenter请求到数据的成功回调if(springView !=null){//刷新完成的方法springView.onFinishFreshAndLoad();}//进入页面给适配器传数据mutiAdapter.addData(mutibean.getResult().getData());}@Overridepublic void failure(Exception e) {Toast.makeText(this,"error",Toast.LENGTH_SHORT).show();}
}
view层的接口public interface MutiViewCallBack {public void success(MutiBean mutibean);//这个方法接收到访问网络传回的数据public void failure(Exception e);
}
presenter层,在调用model层访问数据
public class MutiPresenter {MutiModel mutiModel = new MutiModel();MutiViewCallBack mutiviewCallBack;public MutiPresenter(MutiViewCallBack mutiviewCallBack) {this.mutiviewCallBack = mutiviewCallBack;}//view层调用p这个方法 p层再去model层访问数据public void onRefresh(boolean shuaxin){mutiModel.Refresh(shuaxin, new MutiModel.ModelCallBack() {@Overridepublic void modelsuccess(MutiBean bean) {mutiviewCallBack.success(bean);}@Overridepublic void modelfailure(Exception e) {mutiviewCallBack.failure(e);}});}}
model层里面调用okhttp封装类,访问网络数据,成功的回调中携带bean类,bean类型改成提前写好的bean类
public class MutiModel {//model层里面访问网络数据,调用okhttppublic void Refresh(boolean shuaxin, final ModelCallBack modelCallBack) {//这里面 使用okhttp 封装好的方法请求数据//http://v.juhe.cn/toutiao/index?type=&key=fac0f16b2ab153b436517271e78408ad//http://v.juhe.cn/toutiao/index?type=top&key=597b4f9dcb50e051fd725a9ec54d6653OkhttpUtils.getInstance().asyn(null, "http://v.juhe.cn/toutiao/index?type=&key=fac0f16b2ab153b436517271e78408ad", new AbstractUiCallBack<MutiBean>() {@Override//封装好的okhttputils类里面实现的接口public void success(MutiBean mutiBean) {modelCallBack.modelsuccess(mutiBean);}@Overridepublic void failure(Exception e) {modelCallBack.modelfailure(e);}});}//model的接口public interface ModelCallBack{public void modelsuccess(MutiBean bean);public void modelfailure(Exception e);}
}
这时Mainactivity的成功的回调方法里面有了 bean的数据,将集合传给adapter,设置recyclerview显示
public class MutiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{private Context context;public MutiAdapter(Context context){this.context = context;}//先定义一个装数据的集合,一开始是空的,调用adddata方法时候集合才有数据private List<MutiBean.ResultBean.DataBean> listda;public void addData(List<MutiBean.ResultBean.DataBean> list){//list是传来的集合,里面有请求到的数据if(listda==null){//如果不存在这个集合 就创建一个listda = new ArrayList<>();}//将传来的集合 添加进去listda.addAll(list);//刷新适配器notifyDataSetChanged();}@Override//创建viewholder 的public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//根据 不同的类型 创建不同的viewholderif(viewType == 0){//一张图片View view1 = View.inflate(context, R.layout.muti_item1,null);ViewHolder1 viewHolder1 = new ViewHolder1(view1);return viewHolder1;}else if (viewType==1){//两张图片View view2 = View.inflate(context,R.layout.muti_item2,null);ViewHolder2 viewHolder2 = new ViewHolder2(view2);return viewHolder2;}else{View view3 = View.inflate(context,R.layout.muti_item3,null);ViewHolder3 viewHolder3 = new ViewHolder3(view3);return viewHolder3;}}@Override//显示图片的 判断viewholder的不同,设置控件的值public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {//判断viewholderif(holder instanceof ViewHolder1){ViewHolder1 holder1 = (ViewHolder1) holder;ImageLoader.getInstance().displayImage(listda.get(position).getThumbnail_pic_s(),holder1.item1_image);}else if (holder instanceof ViewHolder2){ViewHolder2 holder2 = (ViewHolder2) holder;ImageLoader.getInstance().displayImage(listda.get(position).getThumbnail_pic_s(),holder2.item2_image1);ImageLoader.getInstance().displayImage(listda.get(position).getThumbnail_pic_s02(),holder2.item2_image2);}else{ViewHolder3 holder3 = (ViewHolder3) holder;ImageLoader.getInstance().displayImage(listda.get(position).getThumbnail_pic_s(),holder3.item3_image1);ImageLoader.getInstance().displayImage(listda.get(position).getThumbnail_pic_s02(),holder3.item3_image2);ImageLoader.getInstance().displayImage(listda.get(position).getThumbnail_pic_s03(),holder3.item3_image3);}}@Overridepublic int getItemViewType(int position) {//返回条目类型的数量,3种条目 返回0,1,2//判断是哪种条目类型if(!TextUtils.isEmpty(listda.get(position).getThumbnail_pic_s03())){return 2;}else if(!TextUtils.isEmpty(listda.get(position).getThumbnail_pic_s02())){return 1;}else{return 0;}}@Overridepublic int getItemCount() {return listda==null?0:listda.size();//返回的条目的数量}//自己写的三个viewholder,获取控件的idpublic static class ViewHolder1 extends RecyclerView.ViewHolder {private final ImageView item1_image;public ViewHolder1(View itemView) {super(itemView);//一张图片的item1_image = (ImageView) itemView.findViewById(R.id.item1_image);}}public static class ViewHolder2 extends RecyclerView.ViewHolder {private final ImageView item2_image1;private final ImageView item2_image2;public ViewHolder2(View itemView) {super(itemView);item2_image1 = (ImageView) itemView.findViewById(R.id.item2_image1);item2_image2 = (ImageView) itemView.findViewById(R.id.item2_image2);}}public static class ViewHolder3 extends RecyclerView.ViewHolder {private final ImageView item3_image1;private final ImageView item3_image2;private final ImageView item3_image3;public ViewHolder3(View itemView) {super(itemView);item3_image1 = (ImageView) itemView.findViewById(R.id.item3_image1);item3_image2 = (ImageView) itemView.findViewById(R.id.item3_image2);item3_image3 = (ImageView) itemView.findViewById(R.id.item3_image3);}}
}
这篇关于MVP操作RecyclerView多条目SpringView,OKHttp封装请求网络的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!