导入依赖,加入网络权限
compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.facebook.fresco:fresco:0.12.0' testCompile 'junit:junit:4.12' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.squareup.okio:okio:1.13.0' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.jcodecraeer:xrecyclerview:1.3.2' compile 'com.google.code.gson:gson:2.8.2' compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' //Rxjava compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.1.7' compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' 布局文件<android.support.v7.widget.RecyclerView android:id="@+id/rlv" android:layout_width="wrap_content" android:layout_height="wrap_content" > </android.support.v7.widget.RecyclerView>
item1:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/barview" android:layout_width="50dp" android:layout_height="50dp" /> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/txt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你好" /> <TextView android:id="@+id/txt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你也好" /> </LinearLayout> </LinearLayout>
MApppublic class MyApp extends Application{ @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }
bean类/** * msg : * code : 0 * data : [{"cid":1,"createtime":"2017-10-10T19:41:39","icon":"http://120.27.23.105/images/category/shop.png","ishome":1,"name":"京东超市"},{"cid":2,"createtime":"2017-10-10T19:41:39","icon":"http://120.27.23.105/images/category/qqg.png","ishome":1,"name":"全球购"},{"cid":3,"createtime":"2017-10-10T19:45:11","icon":"http://120.27.23.105/images/category/phone.png","ishome":1,"name":"手机数码"},{"cid":5,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/man.png","ishome":1,"name":"男装"},{"cid":6,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/girl.png","ishome":1,"name":"女装"},{"cid":7,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/manshoe.png","ishome":1,"name":"男鞋"},{"cid":8,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/girlshoe.png","ishome":1,"name":"女鞋"},{"cid":9,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/shirt.png","ishome":1,"name":"内衣配饰"},{"cid":10,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/bag.png","ishome":1,"name":"箱包手袋"},{"cid":11,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/beauty.png","ishome":1,"name":"美妆个护"},{"cid":12,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/jewel.png","ishome":1,"name":"钟表珠宝"},{"cid":13,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/luxury.png","ishome":1,"name":"奢侈品"},{"cid":14,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/computer.png","ishome":1,"name":"电脑办公"},{"cid":15,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"家用电器"},{"cid":16,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"食品生鲜"},{"cid":17,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"酒水饮料"},{"cid":18,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"母婴童装"},{"cid":19,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"玩具乐器"},{"cid":20,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"医药保健"}] */ private String msg; private String code; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { /** * cid : 1 * createtime : 2017-10-10T19:41:39 * icon : http://120.27.23.105/images/category/shop.png * ishome : 1 * name : 京东超市 */ private int cid; private String createtime; private String icon; private int ishome; private String name; public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public int getIshome() { return ishome; } public void setIshome(int ishome) { this.ishome = ishome; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
网络请求类
SeresAPI类public interface SeresApi { @GET(UrlUtils.TAG) Flowable<UserBean> tags(); }
URIUtils放接口的地方public class UrlUtils { //Host为接口,TAG为接口参数 public static final String Host="http://120.27.23.105/"; public static final String TAG="product/getCatagory"; }
OnNetLiner范型接口public interface OnNerLiner<T> { void onSuccess(T t); void onFailed(String str); }
HttpUtils网络请求public class HttpUtils { private static OkHttpClient okHttpClient; private static SeresApi seresApi; static { inits(); } private static void inits(){ if(okHttpClient==null){ synchronized (OkHttpClient.class){ if(okHttpClient==null){ okHttpClient = new OkHttpClient.Builder().build(); } } } } public static SeresApi getApi(){ //根据网络请求 if(seresApi==null){ synchronized (SeresApi.class){ if(seresApi==null){ seresApi=HttpUtils.createApi(SeresApi.class,UrlUtils.Host); } } } return seresApi; } public static <T> T createApi(Class<T> clazz,String url){ Retrofit retrofit = new Retrofit.Builder().baseUrl(UrlUtils.Host) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return retrofit.create(clazz); } }
View层public interface IMainActivity { public void show(UserBean userBean); }M层public class ShowModel { //m层主要为解析gson public void getshow(final OnNerLiner<UserBean> onNerLiner){ HttpUtils.getApi().tags() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<UserBean>() { @Override public void accept(UserBean userBean) throws Exception { onNerLiner.onSuccess(userBean); } }); } }P层public class ShowPresenter { //p层将m层和v层相连接 private IMainActivity iMainActivity; private ShowModel showModel; public ShowPresenter(IMainActivity iMainActivity){ this.iMainActivity = iMainActivity; showModel = new ShowModel(); } public void show(){ showModel.getshow(new OnNerLiner<UserBean>() { @Override public void onSuccess(UserBean userBean) { //将bean类传过来 iMainActivity.show(userBean); } @Override public void onFailed(String str) { } }); } }
//适配器public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private Context context; private List<UserBean.DataBean> list; private LayoutInflater inflater; public MyAdapter(Context context, List<UserBean.DataBean> list) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.item, parent, false); MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { MyViewHolder myViewHolder = (MyViewHolder) holder; UserBean.DataBean dataBean = list.get(position); myViewHolder.image.setImageURI(dataBean.getIcon()); myViewHolder.tv_title.setText(dataBean.getName()); myViewHolder.tv_time.setText(dataBean.getCreatetime()); } @Override public int getItemCount() { return list.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ //定义 private SimpleDraweeView image; private TextView tv_title; private TextView tv_time; public MyViewHolder(View itemView) { super(itemView); image = itemView.findViewById(R.id.barview); tv_title = itemView.findViewById(R.id.txt1); tv_time = itemView.findViewById(R.id.txt2); } } }
主函数方法public class MainActivity extends AppCompatActivity implements IMainActivity{ private RecyclerView mRev; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); ShowPresenter show = new ShowPresenter(this); show.show(); } private void initView() { mRev = (RecyclerView) findViewById(R.id.rlv); mRev.setLayoutManager(new LinearLayoutManager(this)); } @Override public void show(UserBean userBean) { MyAdapter myadapter = new MyAdapter(this,userBean.getData()); mRev.setAdapter(myadapter); } }