MVP okhttp+springview+RecyclerView 上拉下拉功能+多条目加载
来源:互联网 发布:无线键盘 知乎 编辑:程序博客网 时间:2024/06/06 14:01
这个工程所需要的依赖dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.0.0-alpha1' testCompile 'junit:junit:4.12' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.github.liuguangqiang.SuperRecyclerView:super-recyclerview:0.1.2' compile 'com.jakewharton:butterknife:8.8.1' compile 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.liaoinstan.springview:library:1.2.6' compile files('libs/gson-2.3.1.jar')}----------------------------------清单目录--------------------------------<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.okhttp"> <uses-permission android:name="android.permission.INTERNET" /> <application android:name=".MyApp" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
---------------------------------------布局页面----------------------------------------------
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="app.com.okhttp.MainActivity"> <com.liaoinstan.springview.widget.SpringView android:id="@+id/springview" android:layout_width="match_parent" android:layout_height="match_parent"> <com.liuguangqiang.recyclerview.widget.SuperRecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent"></com.liuguangqiang.recyclerview.widget.SuperRecyclerView> </com.liaoinstan.springview.widget.SpringView></RelativeLayout>
item1.xml
<?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="match_parent" android:orientation="vertical"> <TextView android:id="@+id/list_item1_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1111111" /> <ImageView android:id="@+id/list_item1_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /></LinearLayout>item2.xml
<?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="match_parent" android:orientation="vertical"> <TextView android:id="@+id/list_item2_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2222222222" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/list_item2_imageview1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <ImageView android:id="@+id/list_item2_imageview2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> </LinearLayout></LinearLayout>
item3.xml
<?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="match_parent" android:orientation="vertical"> <TextView android:id="@+id/list_item3_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2222222222" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/list_item3_imageview1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <ImageView android:id="@+id/list_item3_imageview2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <ImageView android:id="@+id/list_item3_imageview3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> </LinearLayout></LinearLayout>
--------------------------------------主页面-------------------------------------
module包 MultilActivityModule.java
package app.com.okhttp.module;import app.com.okhttp.MultiBean;import app.com.okhttp.okhttp.AbstractUiCallBack;import app.com.okhttp.okhttp.OkhttpUtils;/** * Created by muhanxi on 17/11/10. */public class MultilActivityModule { /** * up true 上啦 false 下啦 */ public void onRefresh(boolean up, final ModuleCallBack callBack) { OkhttpUtils.getInstance().asy(null, "http://v.juhe.cn/toutiao/index?type=top&key=597b4f9dcb50e051fd725a9ec54d6653", new AbstractUiCallBack<MultiBean>() { @Override public void success(MultiBean bean) { callBack.success(bean); } @Override public void failure(Exception e) { callBack.failure(e); } }); } public interface ModuleCallBack { public void success(MultiBean bean); public void failure(Exception bean); }}
okhttp包 AbstractUiCallBack.java
package app.com.okhttp.okhttp;import android.os.Handler;import android.os.Looper;import com.google.gson.Gson;import com.google.gson.JsonSyntaxException;import java.io.IOException;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import okhttp3.Call;import okhttp3.Callback;import okhttp3.Response;/** * Created by muhanxi on 17/11/10. * <p> * * Okhttp 单例 范型的封装 */public abstract class AbstractUiCallBack<T> implements Callback { /** * 成功回调 * * @param t */ public abstract void success(T t); /** * 失败回调 * * @param e */ public abstract void failure(Exception e); private Handler handler = null; private Class clazz; public AbstractUiCallBack() { handler = new Handler(Looper.getMainLooper()); // 得到的是一个 AbstractUiCallBack<T> 的Type Type type = getClass().getGenericSuperclass(); // 得到的是T的实际Type Type[] arr = ((ParameterizedType) type).getActualTypeArguments(); clazz = (Class) arr[0]; } @Override public void onFailure(Call call, IOException e) { failure(e); } @Override public void onResponse(Call call, Response response) throws IOException { try { String result = response.body().string(); System.out.println("result = " + result); Gson gson = new Gson(); final T t = (T) gson.fromJson(result, clazz); handler.post(new Runnable() { @Override public void run() { success(t); } }); } catch (IOException e) { e.printStackTrace(); failure(e); } catch (JsonSyntaxException e) { e.printStackTrace(); failure(e); } }}
okhttp包 OkhttpUtils.java
package app.com.okhttp.okhttp;import java.io.File;import java.util.Map;import java.util.concurrent.TimeUnit;import app.com.okhttp.LoggingInterceptor;import okhttp3.FormBody;import okhttp3.MediaType;import okhttp3.MultipartBody;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.RequestBody;/** * Created by muhanxi on 17/11/10. * * * Okhttp 单例 范型的封装 */public class OkhttpUtils { private static OkhttpUtils okhttpUtils = null ; private OkhttpUtils(){ } public static OkhttpUtils getInstance(){ if(okhttpUtils == null){ okhttpUtils = new OkhttpUtils(); client = new OkHttpClient.Builder() .readTimeout(20, TimeUnit.SECONDS) .writeTimeout(20,TimeUnit.SECONDS) .connectTimeout(20,TimeUnit.SECONDS) .addInterceptor(new LoggingInterceptor()) .build(); } return okhttpUtils ; } private static OkHttpClient client ; public void asy(Map<String,String> params,String url,AbstractUiCallBack callBack){ Request request = null ; if(params != null){ FormBody.Builder builder = new FormBody.Builder() ; for(Map.Entry<String,String> entry : params.entrySet()){ builder.add(entry.getKey(),entry.getValue()); } FormBody body = builder.build(); request = new Request.Builder() .url(url) .post(body) .build(); } else { request = new Request.Builder() .url(url) .build(); } client.newCall(request).enqueue(callBack); } private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png"); public static void postFile(Map<String,String> map, String url, File file,AbstractUiCallBack callBack){ String [] array = file.getAbsolutePath().split("\\/"); MultipartBody.Builder builder = new MultipartBody.Builder(); builder.setType(MultipartBody.FORM); for(Map.Entry<String,String> entry : map.entrySet()){ builder.addFormDataPart(entry.getKey(),entry.getValue()); } builder.addFormDataPart("imageFileName",array[array.length-1]); if(file.exists() && file.length() > 0){ builder.addFormDataPart("image",array[array.length-1], RequestBody.create(MEDIA_TYPE_PNG,file)); } MultipartBody body = builder.build() ; Request request = new Request.Builder() .url(url) .post(body) .build(); client.newCall(request).enqueue(callBack); }}
presenter包 MultilActivityPresenter.java
package app.com.okhttp.presenter;import app.com.okhttp.MainActivity;import app.com.okhttp.MultiBean;import app.com.okhttp.MultiIView;import app.com.okhttp.module.MultilActivityModule;/** * Created by muhanxi on 17/11/10. */public class MultilActivityPresenter { private MultiIView view; private MultilActivityModule multilActivityModule; public MultilActivityPresenter(MainActivity view) { this.view = view; this.multilActivityModule = new MultilActivityModule(); } /** * 下啦刷新 */ public void onRefresh(boolean up) { multilActivityModule.onRefresh(up, new MultilActivityModule.ModuleCallBack() { @Override public void success(MultiBean bean) { view.success(bean); } @Override public void failure(Exception e) { view.failure(e); } }); }}
LoggingInterceptor.java
package app.com.okhttp;import java.io.IOException;import okhttp3.Interceptor;import okhttp3.Request;import okhttp3.Response;public class LoggingInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime();// logger.info(String.format("Sending request %s on %s%n%s",// request.url(), chain.connection(), request.headers())); Response response = chain.proceed(request); long t2 = System.nanoTime();// logger.info(String.format("Received response for %s in %.1fms%n%s",// response.request().url(), (t2 - t1) / 1e6d, response.headers())); System.out.println("t2 = " + (t2-t1)); return response; }}
MainActivity.java
package app.com.okhttp;import android.app.Activity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.widget.Toast;import com.liaoinstan.springview.container.DefaultFooter;import com.liaoinstan.springview.container.DefaultHeader;import com.liaoinstan.springview.widget.SpringView;import app.com.okhttp.presenter.MultilActivityPresenter;import butterknife.BindView;import butterknife.ButterKnife;public class MainActivity extends Activity implements MultiIView { @BindView(R.id.recyclerview) RecyclerView recyclerview; @BindView(R.id.springview) SpringView springview; private MultilActivityPresenter presenter; private MultilActivityAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); presenter = new MultilActivityPresenter(this); adapter = new MultilActivityAdapter(this); LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); recyclerview.setLayoutManager(manager); recyclerview.setAdapter(adapter); springview.setHeader(new DefaultHeader(this)); springview.setFooter(new DefaultFooter(this)); springview.setListener(new SpringView.OnFreshListener() { @Override public void onRefresh() { presenter.onRefresh(true); } @Override public void onLoadmore() { presenter.onRefresh(false); } }); presenter.onRefresh(true); } @Override public void success(MultiBean bean) { if (springview != null) { springview.onFinishFreshAndLoad(); } adapter.addData(bean.getResult().getData()); } @Override public void failure(Exception e) { Toast.makeText(MainActivity.this, " error ", Toast.LENGTH_SHORT).show(); }}
MultiBean.java
这里是一个封装类,根据接口内容,自己封装
MultiIView.java
package app.com.okhttp;/** * Created by muhanxi on 17/11/10. */public interface MultiIView { public void success(MultiBean bean); public void failure(Exception e);}
MultilActivityAdapter.java
package app.com.okhttp;import android.content.Context;import android.support.v7.widget.RecyclerView;import android.text.TextUtils;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import com.nostra13.universalimageloader.core.ImageLoader;import java.util.ArrayList;import java.util.List;import butterknife.BindView;import butterknife.ButterKnife;/** * Created by muhanxi on 17/11/10. */public class MultilActivityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<MultiBean.ResultBean.DataBean> list ; public MultilActivityAdapter(Context context) { this.context = context; } public void addData(List<MultiBean.ResultBean.DataBean> list){ if(this.list == null){ this.list = new ArrayList<>(); } this.list.addAll(list); notifyDataSetChanged(); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == 0) { View view1 = LayoutInflater.from(context).inflate(R.layout.item1, parent, false); return new ViewHolder1(view1); } else if (viewType == 1) { View view2 = LayoutInflater.from(context).inflate(R.layout.item2, parent, false); return new ViewHolder2(view2); } else { View view3 = LayoutInflater.from(context).inflate(R.layout.item3, parent, false); return new ViewHolder3(view3); } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if(holder instanceof ViewHolder1){ ViewHolder1 holder1 = (ViewHolder1)holder ; holder1.listItem1Textview.setText(list.get(position).getTitle()); ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder1.listItem1Imageview); } else if(holder instanceof ViewHolder2){ ViewHolder2 holder2 = (ViewHolder2)holder ; holder2.listItem2Textview.setText(list.get(position).getTitle()); ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder2.listItem2Imageview1); ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),holder2.listItem2Imageview2); } else { ViewHolder3 holder3 = (ViewHolder3)holder ; holder3.listItem3Textview.setText(list.get(position).getTitle()); ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder3.listItem3Imageview1); ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),holder3.listItem3Imageview2); ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s03(),holder3.listItem3Imageview3); } } @Override public int getItemViewType(int position) { if(!TextUtils.isEmpty(list.get(position).getThumbnail_pic_s03())){ return 2; }else if(!TextUtils.isEmpty(list.get(position).getThumbnail_pic_s02())){ return 1; }else { return 0; } } @Override public int getItemCount() { return list == null ? 0 : list.size(); } static class ViewHolder1 extends RecyclerView.ViewHolder { @BindView(R.id.list_item1_imageview) ImageView listItem1Imageview; @BindView(R.id.list_item1_textview) TextView listItem1Textview; ViewHolder1(View view) { super(view); ButterKnife.bind(this, view); } } static class ViewHolder2 extends RecyclerView.ViewHolder { @BindView(R.id.list_item2_textview) TextView listItem2Textview; @BindView(R.id.list_item2_imageview1) ImageView listItem2Imageview1; @BindView(R.id.list_item2_imageview2) ImageView listItem2Imageview2; ViewHolder2(View view) { super(view); ButterKnife.bind(this, view); } } static class ViewHolder3 extends RecyclerView.ViewHolder{ @BindView(R.id.list_item3_textview) TextView listItem3Textview; @BindView(R.id.list_item3_imageview1) ImageView listItem3Imageview1; @BindView(R.id.list_item3_imageview2) ImageView listItem3Imageview2; @BindView(R.id.list_item3_imageview3) ImageView listItem3Imageview3; ViewHolder3(View view) { super(view); ButterKnife.bind(this, view); } }}
MyApp.java
package app.com.okhttp;import android.app.Application;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;/** * Created by lenovo on 2017/11/10. */public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration aDefault = ImageLoaderConfiguration.createDefault(getApplicationContext()); ImageLoader.getInstance().init(aDefault); }}
阅读全文
0 0
- MVP okhttp+springview+RecyclerView 上拉下拉功能+多条目加载
- MVP+OKHttp+Recyclerview+Springview下拉刷新上拉加载
- RecyclerView列表九宫格瀑布流,多条目,SpringView实现上拉下拉刷新
- MVP操作RecyclerView多条目SpringView,OKHttp封装请求网络
- recyclerview,okHttp,布局切换,springView上拉刷新下拉加载
- MVP实现Recyclerview加Springview上拉加载下拉刷新点击条目可跳转到购物车
- 使用MVP,OKHttp封装类及其拦截器,RecyclerView,SpringView,ImageLoader实现数据的多条目加载
- mvp+Recyclerview+okhttp+Springview
- 使用MVP注册登录模块+封装的OKhttp,拦截器+QQ第三方登录+RecyclerView+SpringView上拉加载下拉刷新网络数据
- RecyclerView条目跳转+SpringView数据刷新加载+MVP+OKhttp+拦截器+自定义view 实现请求网络数据的二级列表购物车
- MVP简单实现OKHttp+recyclerview+springview
- RecyclerView上拉下拉
- ListView多条目每次加载数字加20和xlistview上拉下拉
- 简单实现上拉下拉,增加头条目和尾条目的RecyclerView
- 实战MVP请求数据OKHttp封装RecyclerView上拉刷新下拉加载
- 上拉下拉加载
- Recyclerview布局多条目加载+上下拉
- MVP+搜索+recyclerview+springview
- 5.7
- 并发Map映射ConcurrentMap
- 5.1
- IE10/IE11兼容视图设置
- 6.3
- MVP okhttp+springview+RecyclerView 上拉下拉功能+多条目加载
- Windows CE的电源管理之三
- 第4章实验题:小写字母转换为大写字母
- ANR
- 习题6.2(3)
- 第三章作业
- C++ 指针和引用
- 监督学习之kNN原理解析
- Unity通过射线快速变化物体颜色