MVP+RecyclerView切换布局+Ok+拦截器

这是一个与OKhttp和RecyclerView相关的小demo,可以搜索(此接口只能输入手机或笔记本),切换布局,如果你不明白的话去看一下京东(大言不惭)。demo用MVP模式写成,OKhttp请求数据,RecyclerView展示数据,SpringView实现刷新和加载更多。闲言少叙,来看图:

界面有点low,大家将就一下哈!下面上代码。

首先是MainActivity。

[java]  view plain  copy
  1. public class MainActivity extends AppCompatActivity implements MainView, View.OnClickListener {  
  2.   
  3.     //默认 page=1  
  4.     int page = 1;  
  5.     private ImageView ivChange;  
  6.     private EditText et;  
  7.     private TextView search;  
  8.     private RecyclerView recycler;  
  9.     private SpringView spring;  
  10.     private List<ShopBean.DataBean> list = new ArrayList<>();  
  11.     private MyPresneter presneter;  
  12.     private MyAdapter adapter;  
  13.     private boolean flag;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         initView();  
  20.         //实例化MyPresenter  
  21.         presneter = new MyPresneter(this);  
  22.         //为RecyclerView设置分割线  
  23.         recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));  
  24.   
  25.   
  26.         /** 
  27.          * 设置头部样式 
  28.          */  
  29.         spring.setHeader(new DefaultHeader(this));  
  30.         /** 
  31.          * 设置底部样式 
  32.          */  
  33.         spring.setFooter(new DefaultFooter(this));  
  34.         /** 
  35.          * 上拉刷新、下拉加载 
  36.          */  
  37.         spring.setListener(new SpringView.OnFreshListener() {  
  38.             @Override  
  39.             public void onRefresh() {  
  40.   
  41.                 page++;//没拉  
  42.                 String s1 = String.valueOf(page);  
  43.                 presneter.getData(s1);  
  44.                 frashAdapter();  
  45.                 Toast.makeText(MainActivity.this, s1, Toast.LENGTH_SHORT).show();  
  46.                 /** 
  47.                  * 重置控件位置,暴露给外部的方法,用于在刷新或者加载完成后调用 
  48.                  */  
  49.                 spring.onFinishFreshAndLoad();  
  50.             }  
  51.   
  52.             @Override  
  53.             public void onLoadmore() {  
  54.                 presneter.getData("1");  
  55.                 frashAdapter();  
  56.                 spring.onFinishFreshAndLoad();  
  57.             }  
  58.         });  
  59.     }  
  60.   
  61.     private void frashAdapter() {  
  62.         if (adapter == null) {  
  63.             recycler.setLayoutManager(new LinearLayoutManager(this));  
  64.             adapter = new MyAdapter(this, list, flag);  
  65.             adapter.notifyDataSetChanged();  
  66.         } else {  
  67.             adapter.notifyDataSetChanged();  
  68.         }  
  69.     }  
  70.   
  71.     /** 
  72.      * 初始化 
  73.      */  
  74.     private void initView() {  
  75.         ivChange = (ImageView) findViewById(R.id.iv_change);  
  76.         et = (EditText) findViewById(R.id.et);  
  77.         search = (TextView) findViewById(R.id.tv_search);  
  78.         recycler = (RecyclerView) findViewById(R.id.recycler);  
  79.         spring = (SpringView) findViewById(R.id.spring);  
  80.         ivChange.setOnClickListener(this);  
  81.         search.setOnClickListener(this);  
  82.     }  
  83.   
  84.     /** 
  85.      * 由于在MyPresenter中的getData()中调用了此方法,所以此时此方法携带数据 
  86.      * @param bean 
  87.      */  
  88.     @Override  
  89.     public void success(ShopBean bean) {  
  90.         //将数据加进集合里,在Adapter中通过构造器传过去  
  91.         list.addAll(bean.getData());  
  92.         //刷新适配器  
  93.         adapter.notifyDataSetChanged();  
  94.     }  
  95.   
  96.     /** 
  97.      * 获取输入框数据的方法 
  98.      */  
  99.     @Override  
  100.     public String getKeywords() {  
  101.         return et.getText().toString().trim();  
  102.     }  
  103.   
  104.   
  105.     /** 
  106.      * 点击事件 
  107.      */  
  108.     @Override  
  109.     public void onClick(View view) {  
  110.         String str = String.valueOf(page);  
  111.         switch (view.getId()) {  
  112.             case R.id.tv_search:  
  113.                 presneter.getData(str);  
  114.                 break;  
  115.             case R.id.iv_change:  
  116.                 change();  
  117.                 break;  
  118.         }  
  119.     }  
  120.   
  121.     /** 
  122.      * 点击变换布局样式 
  123.      */  
  124.     private void change() {  
  125.         if (flag) {  
  126.             //重新设置图标  
  127.             ivChange.setBackgroundResource(R.drawable.lv_icon);  
  128.             LinearLayoutManager manager = new LinearLayoutManager(this);  
  129.             recycler.setLayoutManager(manager);  
  130.             recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));  
  131.             adapter = new MyAdapter(this, list, flag);  
  132.             recycler.setAdapter(adapter);  
  133.             flag = false;  
  134.         } else {  
  135.             ivChange.setBackgroundResource(R.drawable.grid_icon);  
  136.             recycler.setLayoutManager(new GridLayoutManager(this2));  
  137.             recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));  
  138.             adapter = new MyAdapter(this, list, flag);  
  139.             recycler.setAdapter(adapter);  
  140.             flag = true;  
  141.         }  
  142.     }  
  143.   
  144.   
  145.     /** 
  146.      * 防止内存泄漏 
  147.      */  
  148.     @Override  
  149.     protected void onDestroy() {  
  150.         super.onDestroy();  
  151.         presneter.detach();  
  152.     }  
  153. }  
其次是Model层,先来一个接口

[java]  view plain  copy
  1. public interface ModelCallback {  
  2.     /** 
  3.      *  model接口 
  4.      * @param keywords mobile=手机 
  5.      * @param page page=1 
  6.      * @param onNetListener 
  7.      */  
  8.     void getData(String keywords, String page, OnNetListener<ShopBean> onNetListener);  
  9. }  
再来一个MyModel类
[java]  view plain  copy
  1. public class MyModel implements ModelCallback {  
  2.     Handler handler = new Handler(Looper.getMainLooper());  
  3.   
  4.     /** 
  5.      * 实现接口重写的方法 
  6.      * @param keywords      mobile=手机/笔记本 
  7.      * @param page          page=1 
  8.      * @param onNetListener 
  9.      */  
  10.     @Override  
  11.     public void getData(String keywords, String page, final OnNetListener<ShopBean> onNetListener) {  
  12.         OkHttpUtils.getOkHttpUtils().doGet("http://120.27.23.105/product/searchProducts" + "?keywords=" + keywords + "&page=" + page, new Callback() {  
  13.             @Override  
  14.             public void onFailure(Call call, final IOException e) {  
  15.                 handler.post(new Runnable() {  
  16.                     @Override  
  17.                     public void run() {  
  18.                         onNetListener.failure(e);  
  19.                     }  
  20.                 });  
  21.             }  
  22.   
  23.             @Override  
  24.             public void onResponse(Call call, final Response response) throws IOException {  
  25.                 //网络请求后返回的数据  
  26.                 String result = response.body().string();  
  27.                 Gson gson = new Gson();  
  28.                 //用Gson进行解析  
  29.                 final ShopBean shopBean = gson.fromJson(result, ShopBean.class);  
  30.                 handler.post(new Runnable() {  
  31.                     @Override  
  32.                     public void run() {  
  33.                         onNetListener.success(shopBean);  
  34.                     }  
  35.                 });  
  36.             }  
  37.         });  
  38.     }  
  39. }  
现在我们来到V层,这是MyView类
[java]  view plain  copy
  1. public interface MainView {  
  2.     /** 
  3.      * 获取数成功后 
  4.      */  
  5.     void success(ShopBean bean);  
  6.   
  7.     /** 
  8.      * 获取输入框的数据 
  9.      */  
  10.     String getKeywords();  
  11.   
  12.   
  13. }  
现在是P层,MyPresenter类

[java]  view plain  copy
  1. public class MyPresneter {  
  2.     MainView view;//V层  
  3.     MyModel model;//M层  
  4.   
  5.     /** 
  6.      * 因为在MVP模式中V层与M层不直接交互,通过P层间接交互,所以需要通过构造器将V层数据与M层数据传到P层 
  7.      * @param view 
  8.      */  
  9.     public MyPresneter(MainView view) {  
  10.         this.view = view;  
  11.         model = new MyModel();  
  12.     }  
  13.   
  14.     /** 
  15.      * 获取数据 
  16.      * @param num page 
  17.      */  
  18.     public void getData(String page){  
  19.         //获取输入框输入的数据  
  20.         String keywords = view.getKeywords();  
  21.         /** 
  22.          * keywords mobile=手机/笔记本 
  23.          * num page=1 
  24.          */  
  25.         model.getData(keywords, page, new OnNetListener<ShopBean>() {  
  26.             @Override  
  27.             public void success(ShopBean shopBean) {  
  28.                 //至此view里面已经携带了从网络请求来并解析后的数据  
  29.                 view.success(shopBean);  
  30.             }  
  31.   
  32.             @Override  
  33.             public void failure(Exception e) {  
  34.   
  35.             }  
  36.         });  
  37.     }  
  38.   
  39.     /** 
  40.      * 防止内存泄露的方法,在Activity中重写onDestroy方法调用 
  41.      */  
  42.     public void detach(){  
  43.         this.view = null;  
  44.     }  
  45. }  
我们来看一下适配器

[java]  view plain  copy
  1. public class MyAdapter extends RecyclerView.Adapter{  
  2.     Context context;//上下文  
  3.     List<ShopBean.DataBean> list;//包含数据的集合  
  4.     boolean flag;//根据flag判断显示那种布局  
  5.   
  6.     /** 
  7.      * List集合里怎么有数据的呢?请看MainActivity中的show方法 
  8.      */  
  9.     public MyAdapter(Context context,List<ShopBean.DataBean> list,boolean flag) {  
  10.         this.context = context;  
  11.         this.list = list;  
  12.         this.flag = flag;  
  13.     }  
  14.   
  15.     /** 
  16.      * Called when RecyclerView needs a new ViewHolder of the given type to represent 
  17.      * an item. 
  18.      */  
  19.     @Override  
  20.     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
  21.         /** 
  22.          * 如果flag=true加载线性布局,否则加载网格布局 
  23.          */  
  24.         if (flag) {  
  25.             View inflate1 = LayoutInflater.from(context).inflate(R.layout.lv_item_layout, null);  
  26.             return new LvViewHolder(inflate1);  
  27.         }else{  
  28.             View inflate2 = LayoutInflater.from(context).inflate(R.layout.gv_item_layout, null);  
  29.             return new GvViewHolder(inflate2);  
  30.         }  
  31.   
  32.     }  
  33.   
  34.     /** 
  35.      * Called by RecyclerView to display the data at the specified position. This method should 
  36.      * update the contents of the  ViewHolder#itemView to reflect the item at the given 
  37.      * position. 
  38.      */  
  39.     @Override  
  40.     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {  
  41.         /** 
  42.          *如果flag=true设置线性布局的数据,否则设置网格布局的数据 
  43.          */  
  44.         if(flag){  
  45.            if(holder instanceof LvViewHolder){  
  46.                //按照|格式拆分图片路径  
  47.                String[] str = list.get(position).getImages().split("\\|");  
  48.                ImageLoader.getInstance().displayImage(str[0],((LvViewHolder) holder).iv);  
  49.                ((LvViewHolder) holder).title.setText(list.get(position).getTitle());  
  50.                ((LvViewHolder) holder).newPrice.setText("¥"+list.get(position).getPrice()+"");  
  51.                ((LvViewHolder) holder).oldPrice.setText("¥"+list.get(position).getBargainPrice()+"");  
  52.            }  
  53.         }else{  
  54.           if(holder instanceof GvViewHolder){  
  55.               //按照|格式拆分图片路径  
  56.               String[] str = list.get(position).getImages().split("\\|");  
  57.               ImageLoader.getInstance().displayImage(str[0],((GvViewHolder) holder).gIv);  
  58.               ((GvViewHolder) holder).gTitle.setText(list.get(position).getTitle());  
  59.               ((GvViewHolder) holder).gNewPrice.setText("¥"+list.get(position).getPrice()+"");  
  60.               ((GvViewHolder) holder).gPrice.setText("¥"+list.get(position).getBargainPrice()+"");  
  61.           }  
  62.         }  
  63.   
  64.     }  
  65.   
  66.     /** 
  67.      * Returns the number of items in the adapter bound to the parent RecyclerView. 
  68.      * @return 
  69.      */  
  70.     @Override  
  71.     public int getItemCount() {  
  72.         return list==null?0:list.size();  
  73.     }  
  74.   
  75.     /** 
  76.      * A ViewHolder describes an item view and metadata about its place within the RecyclerView. 
  77.      * 线性布局viewHolder 
  78.      */  
  79.      class LvViewHolder extends RecyclerView.ViewHolder{  
  80.          TextView title,newPrice,oldPrice;  
  81.          ImageView iv;  
  82.   
  83.         public LvViewHolder(View itemView) {  
  84.             super(itemView);  
  85.             iv = itemView.findViewById(R.id.iv_lv);  
  86.             title = itemView.findViewById(R.id.tv_lv_title);  
  87.             newPrice=itemView.findViewById(R.id.tv_lv_new_price);  
  88.             oldPrice=itemView.findViewById(R.id.tv_lv_old_price);  
  89.         }  
  90.     }  
  91.   
  92.     /** 
  93.      * A ViewHolder describes an item view and metadata about its place within the RecyclerView. 
  94.      * 网格布局viewHolder 
  95.      */  
  96.     class GvViewHolder extends RecyclerView.ViewHolder{  
  97.         TextView gTitle,gNewPrice,gPrice;  
  98.         ImageView gIv;  
  99.   
  100.         public GvViewHolder(View itemView) {  
  101.             super(itemView);  
  102.             gIv = itemView.findViewById(R.id.image);  
  103.             gTitle = itemView.findViewById(R.id.title);  
  104.             gNewPrice=itemView.findViewById(R.id.new_price);  
  105.             gPrice=itemView.findViewById(R.id.price);  
  106.         }  
  107.     }  
  108.   
  109. }  
现在我们看一下OKhttp吧,

OKhttpUtils,封装了get和post请求

[java]  view plain  copy
  1. public class OkHttpUtils {  
  2.     /** 
  3.      * OKhttp提供的打印log的类 
  4.      */  
  5.   
  6.     static HttpLoggingInterceptor loging;  
  7.     private static OkHttpUtils okHttpUtils = null;  
  8.     private static OkHttpClient client;  
  9.   
  10.     /** 
  11.      * 为单例模式提供构造器 
  12.      */  
  13.     private OkHttpUtils() {  
  14.     }  
  15.   
  16.     /** 
  17.      * 单例模式封装 
  18.      */  
  19.     public static OkHttpUtils getOkHttpUtils() {  
  20.         loging = new HttpLoggingInterceptor();  
  21.         loging.setLevel(HttpLoggingInterceptor.Level.BODY);  
  22.         if (okHttpUtils == null) {  
  23.             okHttpUtils = new OkHttpUtils();  
  24.             client = new OkHttpClient.Builder()  
  25.                     .connectTimeout(20, TimeUnit.SECONDS)  
  26.                     .addInterceptor(loging)  
  27.                     .addInterceptor(new MyInterceptor())  
  28.                     .build();  
  29.         }  
  30.   
  31.         return okHttpUtils;  
  32.   
  33.     }  
  34.   
  35.     /** 
  36.      * GET请求 
  37.      */  
  38.     public void doGet(String url, Callback callback) {  
  39.         Request request = new Request.Builder().url(url).build();  
  40.         client.newCall(request).enqueue(callback);  
  41.   
  42.     }  
  43.   
  44.     /** 
  45.      * POST请求 
  46.      */  
  47.     public void doPost(Map<String, String> params, String url, Callback callback) {  
  48.         FormBody.Builder builder = new FormBody.Builder();  
  49.         for (Map.Entry<String, String> entry : params.entrySet()) {  
  50.             builder.add(entry.getKey(), entry.getValue());  
  51.         }  
  52.         FormBody build = builder.build();  
  53.         Request request = new Request.Builder().url(url).post(build).build();  
  54.         client.newCall(request).enqueue(callback);  
  55.     }  
  56. }  
[java]  view plain  copy
  1. OnNetListener接口  
[java]  view plain  copy
  1. public interface OnNetListener<T> {  
  2.     /** 
  3.      * 请求成功时调用 
  4.      */  
  5.     void success(T t);  
  6.   
  7.     /** 
  8.      * 请求失败时调用 
  9.      */  
  10.     void failure(Exception e);  
  11. }  
拦截器,封装了公共参数

[java]  view plain  copy
  1. public class MyInterceptor implements Interceptor {  
  2.   
  3.     private Request requestProcess;  
  4.     private Response proceed;  
  5.   
  6.     @Override  
  7.     public Response intercept(Chain chain) throws IOException {  
  8.         //获取request  
  9.         Request request = chain.request();  
  10.         //如果是GET请求执行这个方法  
  11.         if ("GET".equals(request.method())) {  
  12.             //为原来的URL添加公共参数  
  13.             String newUrl = request.url().toString() + "&source=android";  
  14.             Request.Builder builder = request.newBuilder();  
  15.             requestProcess = builder.get().url(newUrl).build();  
  16.             proceed = chain.proceed(requestProcess);  
  17.         } else {  
  18.             FormBody.Builder builder = new FormBody.Builder();  
  19.             RequestBody body = request.body();  
  20.             if (body instanceof FormBody) {  
  21.                 FormBody formbody = (FormBody) body;  
  22.                 for (int i = 0; i < formbody.size(); i++) {  
  23.                     builder.add(formbody.encodedName(i), formbody.encodedValue(i));  
  24.                 }  
  25.                 builder.add("source""android");  
  26.             }  
  27.             requestProcess = request.newBuilder().url(request.url().toString()).post(builder.build()).build();  
  28.             proceed = chain.proceed(requestProcess);  
  29.         }  
  30.         return proceed;  
  31.     }  
  32. }  

代码说完了,下面是布局

MainActivity的布局

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context="com.bwie.huang.weektwotest.MainActivity">  
  8.     <LinearLayout  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="35dp"  
  11.         android:orientation="vertical">  
  12.         <RelativeLayout  
  13.             android:layout_width="match_parent"  
  14.             android:layout_height="29dp">  
  15.             <TextView  
  16.                 android:layout_width="match_parent"  
  17.                 android:layout_height="29dp"  
  18.                 android:gravity="center"  
  19.                 android:textSize="20sp"  
  20.                 android:text="搜索商品"  
  21.                 />  
  22.             <ImageView  
  23.                 android:id="@+id/iv_change"  
  24.                 android:layout_width="wrap_content"  
  25.                 android:layout_height="30dp"  
  26.                 android:layout_alignParentRight="true"  
  27.                 android:src="@drawable/lv_icon"/>  
  28.         </RelativeLayout>  
  29.   
  30.         <TextView  
  31.             android:layout_width="match_parent"  
  32.             android:layout_height="1dp"  
  33.             android:background="@android:color/black" />  
  34.     </LinearLayout>  
  35.     <RelativeLayout  
  36.         android:layout_width="match_parent"  
  37.         android:layout_height="50dp"  
  38.         android:gravity="center_vertical">  
  39.         <EditText  
  40.             android:id="@+id/et"  
  41.             android:layout_width="300dp"  
  42.             android:layout_height="30dp"  
  43.             android:layout_marginLeft="15dp"  
  44.             android:background="@drawable/search"  
  45.            />  
  46.         <TextView  
  47.             android:id="@+id/tv_search"  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content"  
  50.             android:layout_alignParentRight="true"  
  51.             android:textSize="25sp"  
  52.             android:layout_marginRight="10dp"  
  53.             android:text="搜索"/>  
  54.     </RelativeLayout>  
  55.     <TextView  
  56.         android:layout_width="match_parent"  
  57.         android:layout_height="1dp"  
  58.         android:background="@android:color/black" />  
  59.     <com.liaoinstan.springview.widget.SpringView  
  60.         android:id="@+id/spring"  
  61.         android:layout_width="match_parent"  
  62.         android:layout_height="match_parent">  
  63.         <android.support.v7.widget.RecyclerView  
  64.             android:id="@+id/recycler"  
  65.             android:layout_width="match_parent"  
  66.             android:layout_height="match_parent"/>  
  67.     </com.liaoinstan.springview.widget.SpringView>  
  68. </LinearLayout>  
lv_item_layout

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@android:color/white"  
  6.     android:padding="10dp">  
  7.     <ImageView  
  8.         android:id="@+id/iv_lv"  
  9.         android:layout_width="0dp"  
  10.         android:layout_weight="1"  
  11.         android:layout_height="60dp"  
  12.         android:src="@mipmap/ic_launcher"/>  
  13.     <LinearLayout  
  14.         android:layout_width="0dp"  
  15.         android:layout_weight="3"  
  16.         android:orientation="vertical"  
  17.         android:padding="10dp"  
  18.         android:layout_height="100dp">  
  19.         <TextView  
  20.             android:id="@+id/tv_lv_title"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="标题"/>  
  24.         <RelativeLayout  
  25.             android:layout_width="match_parent"  
  26.             android:layout_height="wrap_content"  
  27.   
  28.            >  
  29.             <TextView  
  30.                 android:id="@+id/tv_lv_old_price"  
  31.                 android:layout_width="wrap_content"  
  32.                 android:layout_height="wrap_content"  
  33.                 android:text="价格"/>  
  34.             <TextView  
  35.                 android:id="@+id/tv_lv_new_price"  
  36.                 android:layout_width="wrap_content"  
  37.                 android:layout_height="wrap_content"  
  38.                 android:layout_alignParentRight="true"  
  39.                 android:textColor="#ff00"  
  40.                 android:text="折扣价"/>  
  41.         </RelativeLayout>  
  42.     </LinearLayout>  
  43. </LinearLayout>  
gv_item_layout

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:orientation="vertical"  
  5.     android:layout_gravity="center"  
  6.     android:gravity="center"  
  7.     android:background="#fff"  
  8.     android:layout_height="match_parent">  
  9.   
  10.     <ImageView  
  11.         android:id="@+id/image"  
  12.         android:layout_width="100dp"  
  13.         android:src="@mipmap/ic_launcher"  
  14.         android:layout_height="100dp"  
  15.         />  
  16.     <LinearLayout  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:orientation="vertical"  
  20.         >  
  21.         <TextView  
  22.             android:id="@+id/title"  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:text="标题"  
  26.             android:padding="10dp"  
  27.             />  
  28.         <TextView  
  29.             android:id="@+id/price"  
  30.             android:padding="10dp"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="价格"  
  34.             />  
  35.         <TextView  
  36.             android:id="@+id/new_price"  
  37.             android:textColor="#ff0000"  
  38.             android:padding="10dp"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:textSize="15sp"  
  42.             android:text="价格"  
  43.             />  
  44.     </LinearLayout>  
  45.   
  46.   
  47. </LinearLayout>  
位于drawable目录下,用于设置iRecyclerView分割线

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <solid android:color="@android:color/black" />  
  4.     <size  
  5.         android:width="1dp"  
  6.         android:height="1dp" />  
  7. </shape>  
value目录下styles文件

[java]  view plain  copy
  1. <resources>  
  2.     <!-- Base application theme. -->  
  3.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  4.         <!-- Customize your theme here. -->  
  5.         <item name="colorPrimary">@color/colorPrimary</item>  
  6.         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>  
  7.         <item name="colorAccent">@color/colorAccent</item>  
  8.         <!--设置RecyclerView默认分割线-->  
  9.         <item name="android:listDivider">@drawable/diver</item>  
  10.     </style>  
  11. </resources>  
ImageLoader的初始化,不要忘记修改清单文件哦!

[java]  view plain  copy
  1. public class App extends Application {  
  2.     @Override  
  3.     public void onCreate() {  
  4.         super.onCreate();  
  5.         ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();  
  6.         ImageLoader.getInstance().init(configuration);  
  7.     }  
  8. }  

HttpLoggingInterceptor loging;OKhttp提供的打印log的类
compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'//我叫依赖
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值