主Activity
public class MainActivity extends Activity implements MultiIView { private MultilActivityPresenter presenter; private MultilActivityAdapter adapter; private RecyclerView recyclerview; private SpringView springview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); recyclerview = findViewById(R.id.recyclerview); springview = findViewById(R.id.springview); 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(MultilBean bean) { if(springview != null){ springview.onFinishFreshAndLoad(); } adapter.addData(bean.getResult().getData()); } @Override public void failure(Exception e) { Toast.makeText(this, " error ", Toast.LENGTH_SHORT).show(); } }
主布局
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.zhangyongbo.zkmnborther.MainActivity"> <com.liaoinstan.springview.widget.SpringView android:id="@+id/springview" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </com.liaoinstan.springview.widget.SpringView> </RelativeLayout>
View层
public interface MultiIView { public void success(MultilBean bean); public void failure(Exception e); }
Presenter层
public class MultilActivityPresenter { private MultiIView view; private MultilActivityModule multilActivityModule ; public MultilActivityPresenter(MultiIView view){ this.view = view; this.multilActivityModule = new MultilActivityModule(); } /** * 下啦刷新 */ public void onRefresh(boolean up) { multilActivityModule.onRefresh(up, new MultilActivityModule.ModuleCallBack() { @Override public void success(MultilBean bean) { view.success(bean); } @Override public void failure(Exception e) { view.failure(e); } }); } }Moudlec层
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<MultilBean>() { @Override public void success(MultilBean bean) { callBack.success(bean); } @Override public void failure(Exception e) { callBack.failure(e); } }); } public interface ModuleCallBack { public void success(MultilBean bean); public void failure(Exception bean); } }适配器
public class MultilActivityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<MultilBean.ResultBean.DataBean> list ; public MultilActivityAdapter(Context context) { this.context = context; } public void addData(List<MultilBean.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 { ImageView listItem1Imageview; TextView listItem1Textview; ViewHolder1(View view) { super(view); listItem1Imageview = view .findViewById(R.id.itme_view1); listItem1Textview = view .findViewById(R.id.itme_text1); } } static class ViewHolder2 extends RecyclerView.ViewHolder { TextView listItem2Textview; ImageView listItem2Imageview1; ImageView listItem2Imageview2; ViewHolder2(View view) { super(view); listItem2Textview = view .findViewById(R.id.itme_text2); listItem2Imageview1 = view .findViewById(R.id.itme_view2); listItem2Imageview2 = view .findViewById(R.id.itme_view_2); } } static class ViewHolder3 extends RecyclerView.ViewHolder{ TextView listItem3Textview; ImageView listItem3Imageview1; ImageView listItem3Imageview2; ImageView listItem3Imageview3; ViewHolder3(View view) { super(view); listItem3Textview = view .findViewById(R.id.itme_text3); listItem3Imageview1 = view .findViewById(R.id.itme_view3); listItem3Imageview2 = view .findViewById(R.id.itme_view_3); listItem3Imageview3 = view .findViewById(R.id.itme_views3); } } }拦截器
public class LoggingInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); Response response = chain.proceed(request); long t2 = System.nanoTime(); System.out.println("t2 = " + (t2-t1)); return response; } }这是多条目用到的布局文件第一个<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/itme_view1" android:layout_width="120dp" android:layout_height="120dp" /> <TextView android:id="@+id/itme_text1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>第二个<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/itme_view_2" android:layout_width="120dp" android:layout_height="120dp" /> <ImageView android:id="@+id/itme_view2" android:layout_width="120dp" android:layout_height="120dp" /> <TextView android:id="@+id/itme_text2" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>第三个<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/itme_view_3" android:layout_width="120dp" android:layout_height="120dp" /> <ImageView android:id="@+id/itme_view3" android:layout_width="120dp" android:layout_height="120dp" /> <ImageView android:id="@+id/itme_views3" android:layout_width="120dp" android:layout_height="120dp" /> <TextView android:id="@+id/itme_text3" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>温馨提示:记得导依赖,下面是依赖
compile 'com.android.support:appcompat-v7:26.0.0-alpha1' compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.liaoinstan.springview:library:1.2.6' compile files('libs/gson-2.2.4.jar') compile 'com.jakewharton:butterknife:8.1.0' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.okio:okio:1.7.0' compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'用ImageLoade记得导架包和配置文件和去清单文件配置,如果用butterknife报空指针那就自己去findViewById