MVP登录、注册、搜索展示案例

gradle

androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'com.squareup.okhttp3:okhttp:3.3.0'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.android.support:recyclerview-v7:26.1.0'
    compile 'com.github.bumptech.glide:glide:3.6.1'

//权限

 <uses-permission android:name="android.permission.INTERNET"/>

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".view.MainActivity">
        </activity>
        <activity android:name=".view.ContentActivity">
        </activity>
        <activity android:name=".view.RegActivity">
        </activity>
        <activity android:name=".view.GoodsActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

//main布局LinearLayout   android:orientation="vertical"

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="登录"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="0.75dp"
        android:background="#999999"/>
    <EditText
        android:id="@+id/mobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"/>

        <Button
            android:id="@+id/reg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"/>
    </LinearLayout>

//activity_content   LinearLayout   android:orientation="vertical"


    <com.gjl.zhoukao2.view.MySearchView
        android:id="@+id/myseacheView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></com.gjl.zhoukao2.view.MySearchView>

    <!--流式布局-->
    <com.gjl.zhoukao2.view.FlowLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#999999"
            android:text="紧急应急电源"
            android:textSize="25sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#999999"
            android:text="电脑"
            android:textSize="25sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#999999"
            android:text="手机"
            android:textSize="25sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#999999"
            android:text="无敌吹风机"
            android:textSize="25sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#999999"
            android:text="飞机"
            android:textSize="25sp"/>
    </com.gjl.zhoukao2.view.FlowLayout>

    <Button
        android:id="@+id/qingkong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="50dp"
        android:gravity="center"
        android:text="清空历史记录"/>


//activity_goods    LinearLayout   android:orientation="vertical"

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="商品搜索"
            android:textSize="25sp"/>
        <ImageView
            android:id="@+id/change"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/grid"
            android:clickable="true"/>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.75dp"
        android:background="#999999"/>
    <com.gjl.zhoukao2.view.MySearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></com.gjl.zhoukao2.view.MySearchView>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.75dp"/>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        ></android.support.v7.widget.RecyclerView>


//activity_reg 

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="注册"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="0.75dp"
        android:background="#999999"/>
    <EditText
        android:id="@+id/mobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
        <Button
            android:id="@+id/reg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"/>
    </LinearLayout>


//content_top  LinearLayout  android:orientation="horizontal"

 <EditText
        android:id="@+id/et_ss"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:drawableLeft="@drawable/ic_rr"
        android:drawableRight="@drawable/ic_ss"
        android:focusable="false"/>
    <TextView
        android:id="@+id/ss_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="搜做"/>


// item    LinearLayout  android:orientation="horizontal"

  <ImageView
        android:id="@+id/item_iv"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/item_sumary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="简介"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/item_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="原价999"
                />
            <TextView
                android:id="@+id/item_price2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="折扣价999"
                />
        </LinearLayout>
    </LinearLayout>

//model
//Goods
import java.util.List;
public class Goods {
 /**
  * msg : 查询成功
  * code : 0
  * data : [{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","itemtype":0,"pid":57,"price":5199,"pscid":40,"salenum":4343,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","itemtype":1,"pid":58,"price":6399,"pscid":40,"salenum":545,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"},{"bargainPrice":5599,"createtime":"2017-10-10T17:30:32","detailUrl":"https://item.m.jd.com/product/4824715.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n12/jfs/t7768/184/1153704394/148460/f42e1432/599a930fN8a85626b.jpg!q70.jpg","itemtype":0,"pid":59,"price":5599,"pscid":40,"salenum":675,"sellerid":3,"subhead":"游戏本选择4G独显,拒绝掉帧】升级版IPS全高清防眩光显示屏,WASD方向键颜色加持,三大出风口立体散热!","title":"戴尔DELL灵越游匣15PR-6648B GTX1050 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 128GSSD+1T 4G独显 IPS)黑"},{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","itemtype":2,"pid":60,"price":13888,"pscid":40,"salenum":466,"sellerid":4,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","itemtype":1,"pid":61,"price":14999,"pscid":40,"salenum":5535,"sellerid":5,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","itemtype":0,"pid":62,"price":15999,"pscid":40,"salenum":43,"sellerid":6,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","itemtype":1,"pid":63,"price":10000,"pscid":40,"salenum":3232,"sellerid":7,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-03T23:43:53","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","itemtype":0,"pid":64,"price":11000,"pscid":40,"salenum":0,"sellerid":8,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","itemtype":2,"pid":65,"price":12000,"pscid":40,"salenum":868,"sellerid":9,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","itemtype":1,"pid":66,"price":13000,"pscid":40,"salenum":7676,"sellerid":10,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}]
  * page : 1
  */
 private String msg;
 private String code;
 private String page;
 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 String getPage() {
  return page;
 }
 public void setPage(String page) {
  this.page = page;
 }
 public List<DataBean> getData() {
  return data;
 }
 public void setData(List<DataBean> data) {
  this.data = data;
 }
 public static class DataBean {
  /**
   * bargainPrice : 11800
   * createtime : 2017-10-10T17:33:37
   * detailUrl : https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends
   * images : https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg
   * itemtype : 0
   * pid : 57
   * price : 5199
   * pscid : 40
   * salenum : 4343
   * sellerid : 1
   * subhead : 【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统
   * title : 小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银
   */
  private int bargainPrice;
  private String createtime;
  private String detailUrl;
  private String images;
  private int itemtype;
  private int pid;
  private int price;
  private int pscid;
  private int salenum;
  private int sellerid;
  private String subhead;
  private String title;
  public int getBargainPrice() {
   return bargainPrice;
  }
  public void setBargainPrice(int bargainPrice) {
   this.bargainPrice = bargainPrice;
  }
  public String getCreatetime() {
   return createtime;
  }
  public void setCreatetime(String createtime) {
   this.createtime = createtime;
  }
  public String getDetailUrl() {
   return detailUrl;
  }
  public void setDetailUrl(String detailUrl) {
   this.detailUrl = detailUrl;
  }
  public String getImages() {
   return images;
  }
  public void setImages(String images) {
   this.images = images;
  }
  public int getItemtype() {
   return itemtype;
  }
  public void setItemtype(int itemtype) {
   this.itemtype = itemtype;
  }
  public int getPid() {
   return pid;
  }
  public void setPid(int pid) {
   this.pid = pid;
  }
  public int getPrice() {
   return price;
  }
  public void setPrice(int price) {
   this.price = price;
  }
  public int getPscid() {
   return pscid;
  }
  public void setPscid(int pscid) {
   this.pscid = pscid;
  }
  public int getSalenum() {
   return salenum;
  }
  public void setSalenum(int salenum) {
   this.salenum = salenum;
  }
  public int getSellerid() {
   return sellerid;
  }
  public void setSellerid(int sellerid) {
   this.sellerid = sellerid;
  }
  public String getSubhead() {
   return subhead;
  }
  public void setSubhead(String subhead) {
   this.subhead = subhead;
  }
  public String getTitle() {
   return title;
  }
  public void setTitle(String title) {
   this.title = title;
  }
 }
}

//GoodsListener
public interface GoodsListener {
 void getGoodsSuccess(String json);
 void getGoodsError(String error);
}

//IModel
public interface IModel {
 void login(User user, LoginListener loginListener);
 void register(User user, RegListener regListener);
 //获取商品
 void getGoods(String goodName, String page,GoodsListener goodsListener);
}

//LoginListener

public interface LoginListener {
 void loginSucess(String json);
 void loginError(String error);
}

//ModelImpl


import com.gjl.zhoukao2.utils.HttpConig;
import com.gjl.zhoukao2.utils.OkHttpUtils;
import org.json.JSONException;
import org.json.JSONObject;


public class ModelImpl implements IModel {
 @Override
 public void login(User user, final LoginListener loginListener) {
  //获取手机号  密码
  String mobile = user.getMobile();
  String password = user.getPassword();
  //请求网络
  OkHttpUtils okHttpUtils = new OkHttpUtils();
  okHttpUtils.setOkHttpLoadListener(new OkHttpLoadListener() {
   @Override
   public void loadSuccess(String json) {
    //登录的接口
    try {
     JSONObject obj = new JSONObject(json);
     String code = obj.getString("code");
     if (code.equals("0")) {
      loginListener.loginSucess(json);
     } else {
      loginListener.loginError("失败");
     }
    } catch (JSONException e) {
     e.printStackTrace();
    }
   }
   @Override
   public void loadError(String error) {
    loginListener.loginError(error);
   }
  });
  //如果成功,
  okHttpUtils.okHttpGet(HttpConig.login_url + "?mobile=" + mobile + "&password=" + password);
  //失败
 }
 @Override
 public void register(User user, final RegListener regListener) {
//获取手机号  密码
  String mobile = user.getMobile();
  String password = user.getPassword();
  //请求网络
  OkHttpUtils okHttpUtils = new OkHttpUtils();
  okHttpUtils.setOkHttpLoadListener(new OkHttpLoadListener() {
   @Override
   public void loadSuccess(String json) {
    //注册的接口
    try {
     JSONObject obj = new JSONObject(json);
     String code = obj.getString("code");
     if (code.equals("0")) {
      regListener.regSuccess();
     } else {
      regListener.regError();
     }
    } catch (JSONException e) {
     e.printStackTrace();
    }
   }
   @Override
   public void loadError(String error) {
    regListener.regError();
   }
  });
  //如果成功,
  okHttpUtils.okHttpGet(HttpConig.reg_url + "?mobile=" + mobile + "&password=" + password);
  //失败
 }
 @Override
 public void getGoods(String goodName, String page, final GoodsListener goodsListener) {
  OkHttpUtils okHttpUtils = new OkHttpUtils();
  okHttpUtils.setOkHttpLoadListener(new OkHttpLoadListener() {
   @Override
   public void loadSuccess(String json) {
    goodsListener.getGoodsSuccess(json);
   }
   @Override
   public void loadError(String error) {
    goodsListener.getGoodsError(error);
   }
  });
  okHttpUtils.okHttpGet(HttpConig.goods_url + "?keywords=" + goodName + "&page=" + page);
 }

}


//OkHttpLoadListener

public interface OkHttpLoadListener {
 void loadSuccess(String json);
 void loadError(String error);
}


//RegListener

public interface RegListener {
 void regSuccess();
 void regError();
}

//User
public class User {
 private String mobile;
 private String password;
 public User(String mobile, String password) {
  this.mobile = mobile;
  this.password = password;
 }
 public String getMobile() {
  return mobile;
 }
 public void setMobile(String mobile) {
  this.mobile = mobile;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 @Override
 public String toString() {
  return "User{" +
    "mobile='" + mobile + '\'' +
    ", password='" + password + '\'' +
    '}';
 }
}

//presenter
//IPresenter

public interface IPresenter {
 void login(IModel iModel, IView iView);
 void reg(IModel iModel, IView iView);
 void setData(Context context,IModel iModel, IGoodView iGoodView);
 void changeView(IModel iModel, IGoodView iGoodView);
}

//PresenterImpl
public class PresenterImpl implements IPresenter {
 //登录
 @Override
 public void login(IModel iModel, final IView iView) {
  iModel.login(new User(iView.getMobile(), iView.getPassword()), new LoginListener() {
   @Override
   public void loginSucess(String json) {
    iView.jumpActivity();
   }
   @Override
   public void loginError(String error) {
    iView.showError();
   }
  });
 }
 @Override
 public void reg(IModel iModel, final IView iView) {
  iModel.register(new User(iView.getMobile(), iView.getPassword()), new RegListener() {
   @Override
   public void regSuccess() {
    iView.jumpActivity();
   }
   @Override
   public void regError() {
    iView.showError();
   }
  });
 }
 @Override
 public void setData(final Context context, IModel iModel, final IGoodView iGoodView) {
  Toast.makeText(context,"开始--",Toast.LENGTH_SHORT).show();
  iModel.getGoods("笔记本", "1", new GoodsListener() {
   @Override
   public void getGoodsSuccess(String json) {
    Toast.makeText(context,"成功--",Toast.LENGTH_SHORT).show();
    Gson gson = new Gson();
    Goods goods = gson.fromJson(json, Goods.class);
    List<Goods.DataBean> list = goods.getData();
    Toast.makeText(context,"成功--"+list.toString(),Toast.LENGTH_SHORT).show();
    iGoodView.setData(context,list);
   }
   @Override
   public void getGoodsError(String error) {
    Toast.makeText(context,"shibai--",Toast.LENGTH_SHORT).show();
    iGoodView.showError();
   }
  });
 }
 @Override
 public void changeView(IModel iModel, IGoodView iGoodView) {
 }
 //
}

//utils
//HttpConig

public class HttpConig {
 public static String login_url = "https://www.zhaoapi.cn/user/login";
 public static String reg_url = "https://www.zhaoapi.cn/user/reg";
 public static String goods_url = "https://www.zhaoapi.cn/product/searchProducts";
}

//OkHttpUtils

import android.os.Handler;
import android.os.Message;
import com.gjl.zhoukao2.model.OkHttpLoadListener;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpUtils {
 private MyHandler myHandler = new MyHandler();
 private OkHttpLoadListener okHttpLoadListener;
 //get请求
 public void okHttpGet(String url) {
  OkHttpClient client = new OkHttpClient();
  Request builder = new Request.Builder().url(url).build();
  Call call = client.newCall(builder);
  call.enqueue(new Callback() {
   @Override
   public void onFailure(Call call, IOException e) {
    Message message = myHandler.obtainMessage();
    message.what = 0;
    message.obj = "失败";
    myHandler.sendMessage(message);
   }
   @Override
   public void onResponse(Call call, Response response) throws IOException {
    Message message = myHandler.obtainMessage();
    message.what = 1;
    message.obj = response.body().string();
    myHandler.sendMessage(message);
   }
  });
 }
 class MyHandler extends Handler {
  @Override
  public void handleMessage(Message msg) {
   int i = msg.what;
   switch (i) {
    case 0:
     String s = (String) msg.obj;
     //调用接口
     okHttpLoadListener.loadError(s);
     break;
    case 1:
     String josn = (String) msg.obj;
     okHttpLoadListener.loadSuccess(josn);
     break;
   }
  }
 }
 public void setOkHttpLoadListener(OkHttpLoadListener okHttpLoadListener) {
  this.okHttpLoadListener = okHttpLoadListener;
 }

}

//view
//ContentActivity

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.gjl.zhoukao2.R;


public class ContentActivity extends AppCompatActivity{
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_content);
  MySearchView mySearchView = findViewById(R.id.myseacheView);
  mySearchView.setMySeacheViewListener(new MySearchView.MySeacheViewListener() {
   @Override
   public void jump() {
    startActivity(new Intent(ContentActivity.this, GoodsActivity.class));
   }
  });

 }
}


//FlowLayout
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

public class FlowLayout extends ViewGroup {
 public FlowLayout(Context context) {
  this(context, null);
 }
 public FlowLayout(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }
 public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
 }
 private List<List<View>> mAllViews = new ArrayList<List<View>>();
 /**
  * 记录每一行的最大高度
  */
 private List<Integer> mLineHeight = new ArrayList<Integer>();
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  mAllViews.clear();
  mLineHeight.clear();
  int width = getWidth();
  int lineWidth = 0;
  int lineHeight = 0;
  // 存储每一行所有的childView
  List<View> lineViews = new ArrayList<View>();
  int cCount = getChildCount();
  // 遍历所有的孩子
  for (int i = 0; i < cCount; i++) {
   View child = getChildAt(i);
   MarginLayoutParams lp = (MarginLayoutParams) child
     .getLayoutParams();
   int childWidth = child.getMeasuredWidth();
   int childHeight = child.getMeasuredHeight();
   // 如果已经需要换行
   if (childWidth + lp.leftMargin + lp.rightMargin + lineWidth > width) {
    // 记录这一行所有的View以及最大高度
    mLineHeight.add(lineHeight);
    // 将当前行的childView保存,然后开启新的ArrayList保存下一行的childView
    mAllViews.add(lineViews);
    lineWidth = 0;// 重置行宽
    lineViews = new ArrayList<View>();
   }
   /**
    * 如果不需要换行,则累加
    */
   lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
   lineHeight = Math.max(lineHeight, childHeight + lp.topMargin
     + lp.bottomMargin);
   lineViews.add(child);
  }
  // 记录最后一行
  mLineHeight.add(lineHeight);
  mAllViews.add(lineViews);
  int left = 0;
  int top = 0;
  // 得到总行数
  int lineNums = mAllViews.size();
  for (int i = 0; i < lineNums; i++) {
   // 每一行的所有的views
   lineViews = mAllViews.get(i);
   // 当前行的最大高度
   lineHeight = mLineHeight.get(i);

   // 遍历当前行所有的View
   for (int j = 0; j < lineViews.size(); j++) {
    View child = lineViews.get(j);
    if (child.getVisibility() == View.GONE) {
     continue;
    }
    MarginLayoutParams lp = (MarginLayoutParams) child
      .getLayoutParams();
    //计算childView的left,top,right,bottom
    int lc = left + lp.leftMargin;
    int tc = top + lp.topMargin;
    int rc = lc + child.getMeasuredWidth();
    int bc = tc + child.getMeasuredHeight();
    child.layout(lc, tc, rc, bc);
    left += child.getMeasuredWidth() + lp.rightMargin
      + lp.leftMargin;
   }
   left = 0;
   top += lineHeight;
  }
 }
 //处理margin
 @Override
 public LayoutParams generateLayoutParams(AttributeSet attrs) {
  return new MarginLayoutParams(getContext(), attrs);
 }
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  // 获得它的父容器为它设置的测量模式和大小
  int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
  int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
  int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
  int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
  // 如果是warp_content情况下,记录宽和高
  int width = 0;
  int height = 0;
  /**
   * 记录每一行的宽度,width不断取最大宽度
   */
  int lineWidth = 0;
  /**
   * 每一行的高度,累加至height
   */
  int lineHeight = 0;
  int cCount = getChildCount();
  // 遍历每个子元素
  for (int i = 0; i < cCount; i++) {
   View child = getChildAt(i);
   // 测量每一个child的宽和高
   measureChild(child, widthMeasureSpec, heightMeasureSpec);
   // 得到child的lp
   MarginLayoutParams lp = (MarginLayoutParams) child
     .getLayoutParams();
   // 当前子空间实际占据的宽度
   int childWidth = child.getMeasuredWidth() + lp.leftMargin
     + lp.rightMargin;
   // 当前子空间实际占据的高度
   int childHeight = child.getMeasuredHeight() + lp.topMargin
     + lp.bottomMargin;
   /**
    * 如果加入当前child,则超出最大宽度,则的到目前最大宽度给width,类加height 然后开启新行
    */
   if (lineWidth + childWidth > sizeWidth) {
    width = Math.max(lineWidth, childWidth);// 取最大的
    lineWidth = childWidth; // 重新开启新行,开始记录
    // 叠加当前高度,
    height += lineHeight;
    // 开启记录下一行的高度
    lineHeight = childHeight;
   } else
   // 否则累加值lineWidth,lineHeight取最大高度
   {
    lineWidth += childWidth;
    lineHeight = Math.max(lineHeight, childHeight);
   }
   // 如果是最后一个,则将当前记录的最大宽度和当前lineWidth做比较
   if (i == cCount - 1) {
    width = Math.max(width, lineWidth);
    height += lineHeight;
   }
  }
  setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth
    : width, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight
    : height);
 }
}


//GoodsActivity

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.gjl.zhoukao2.R;
import com.gjl.zhoukao2.model.Goods;
import com.gjl.zhoukao2.model.ModelImpl;
import com.gjl.zhoukao2.presenter.PresenterImpl;
import java.util.List;

public class GoodsActivity extends AppCompatActivity implements IGoodView, View.OnClickListener {
 private RecyclerView recyclerView;
 private ImageView change;
 private PresenterImpl presenter;
 private ListView listView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_goods);
  initViews();
  presenter = new PresenterImpl();
  presenter.setData(GoodsActivity.this, new ModelImpl(), this);
 }
 private void initViews() {
  recyclerView = findViewById(R.id.recyclerView);
  listView = findViewById(R.id.listView);
  change = findViewById(R.id.change);
  change.setOnClickListener(this);
 }
 @Override
 public void setData(Context context, List<Goods.DataBean> list) {
  MyAdapter myAdapter = new MyAdapter(context, list);
  recyclerView.setLayoutManager(new LinearLayoutManager(context));
  recyclerView.setAdapter(myAdapter);
 }
 boolean flag = true;
 @Override
 public void changeView() {
  if (flag) {
   change.setImageResource(R.drawable.lv);
   recyclerView.setLayoutManager(new GridLayoutManager(GoodsActivity.this, 2));
  } else {
   change.setImageResource(R.drawable.grid);
   recyclerView.setLayoutManager(new LinearLayoutManager(GoodsActivity.this));
  }
  flag = !flag;
 }
 @Override
 public void showError() {
  Toast.makeText(GoodsActivity.this, "失败", Toast.LENGTH_SHORT).show();
 }
 @Override
 public void onClick(View v) {
  presenter.changeView(new ModelImpl(), this);
 }
}

//IGoodView

import android.content.Context;
import com.gjl.zhoukao2.model.Goods;
import java.util.List;

public interface IGoodView {
 void setData(Context context,List<Goods.DataBean> list);
 void changeView();
 void showError();
}

//IView
public interface IView {
 String getMobile();
 String getPassword();
 void jumpActivity();
 void showError();
}

//MainActivity
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.gjl.zhoukao2.R;
import com.gjl.zhoukao2.model.ModelImpl;
import com.gjl.zhoukao2.presenter.PresenterImpl;

public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener {
 private EditText mobile;
 private EditText password;
 private Button login;
 private Button reg;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //初始化界面
  initViews();
 }
 private void initViews() {
  mobile = findViewById(R.id.mobile);
  password = findViewById(R.id.password);
  login = findViewById(R.id.login);
  reg = findViewById(R.id.reg);
  login.setOnClickListener(this);
  reg.setOnClickListener(this);
 }
 //实现IView的方法
 @Override
 public String getMobile() {
  return mobile.getText().toString();
 }
 @Override
 public String getPassword() {
  return password.getText().toString();
 }
 @Override
 public void jumpActivity() {
  startActivity(new Intent(MainActivity.this, ContentActivity.class));
 }
 @Override
 public void showError() {
  Toast.makeText(MainActivity.this, "失败请注册", Toast.LENGTH_SHORT).show();
 }
 //点击事件
 @Override
 public void onClick(View v) {
  switch (v.getId()) {
   case R.id.login:
    PresenterImpl presenter = new PresenterImpl();
    presenter.login(new ModelImpl(),this);
    break;
   case R.id.reg:
    startActivity(new Intent(MainActivity.this,RegActivity.class));
    break;
  }
 }
}

//MyAdapter
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.gjl.zhoukao2.R;
import com.gjl.zhoukao2.model.Goods;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
 private final List<Goods.DataBean> list;
 private final Context context;
 public MyAdapter(Context context, List<Goods.DataBean> list) {
  this.context = context;
  this.list = list;
 }
 @Override
 public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  View view = LayoutInflater.from(context).inflate(R.layout.item, parent,false);
  MyViewHolder myViewHolder = new MyViewHolder(view);
  return myViewHolder;
 }
 @Override
 public void onBindViewHolder(MyViewHolder holder, int position) {
  //
  Glide.with(context).load(list.get(position).getImages()).into(holder.getImageView());
  holder.getSumary().setText(list.get(position).getTitle());
  holder.getItem_price().setText(list.get(position).getPrice() + "");
  holder.getItem_price2().setText(list.get(position).getPrice() * 0.8 + "");
 }
 @Override
 public int getItemCount() {
  return 0;
 }
 class MyViewHolder extends RecyclerView.ViewHolder {
  private final ImageView imageView;
  private final TextView sumary;
  private final TextView item_price;
  private final TextView item_price2;
  public MyViewHolder(View itemView) {
   super(itemView);
   imageView = itemView.findViewById(R.id.item_iv);
   sumary = imageView.findViewById(R.id.item_sumary);
   item_price = imageView.findViewById(R.id.item_price);
   item_price2 = imageView.findViewById(R.id.item_price2);
  }
  public ImageView getImageView() {
   return imageView;
  }
  public TextView getSumary() {
   return sumary;
  }
  public TextView getItem_price() {
   return item_price;
  }
  public TextView getItem_price2() {
   return item_price2;
  }
 }
}


//MySearchView
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import com.gjl.zhoukao2.R;

public class MySearchView extends LinearLayout {
 private MySeacheViewListener mySeacheViewListener;
 public MySearchView(Context context) {
  this(context,null);
 }
 public MySearchView(Context context, @Nullable AttributeSet attrs) {
  this(context, attrs,0);
 }
 public MySearchView(final Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  View view = LayoutInflater.from(context).inflate(R.layout.content_top, this, true);
  view.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    mySeacheViewListener.jump();
   }
  });
 }
 public interface MySeacheViewListener{
  void jump();
 }
 public void setMySeacheViewListener(MySeacheViewListener mySeacheViewListener){
  this.mySeacheViewListener = mySeacheViewListener;
 }
}


//RegActivity
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.gjl.zhoukao2.R;
import com.gjl.zhoukao2.model.ModelImpl;
import com.gjl.zhoukao2.presenter.PresenterImpl;


public class RegActivity extends AppCompatActivity implements View.OnClickListener, IView {
 private EditText mobile;
 private android.widget.EditText password;
 private Button reg;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_reg);
  //初始化界面
  initViews();
 }
 private void initViews() {
  mobile = findViewById(R.id.mobile);
  password = findViewById(R.id.password);
  reg = findViewById(R.id.reg);
  reg.setOnClickListener(this);
 }
 @Override
 public void onClick(View v) {
  PresenterImpl presenter = new PresenterImpl();
  presenter.reg(new ModelImpl(), this);
 }
 @Override
 public String getMobile() {
  return mobile.getText().toString();
 }
 @Override
 public String getPassword() {
  return password.getText().toString();
 }
 @Override
 public void jumpActivity() {
  startActivity(new Intent(RegActivity.this, ContentActivity.class));
 }
 @Override
 public void showError() {
  Toast.makeText(RegActivity.this, "失败请注册", Toast.LENGTH_SHORT).show();
 }
}







 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值