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"/>
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 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"/>
</intent-filter>
</activity>
</application>
//main布局LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="登录"
/>
android:layout_width="match_parent"
android:layout_height="0.75dp"
android:id="@+id/mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
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">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#999999"
android:text="紧急应急电源"
android:textSize="25sp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#999999"
android:text="电脑"
android:textSize="25sp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#999999"
android:text="手机"
android:textSize="25sp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#999999"
android:text="无敌吹风机"
android:textSize="25sp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#999999"
android:text="飞机"
android:textSize="25sp"/>
<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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="商品搜索"
android:textSize="25sp"/>
android:id="@+id/change"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/grid"
android:clickable="true"/>
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="0.75dp"
android:background="#999999"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.gjl.zhoukao2.view.MySearchView>
android:layout_width="match_parent"
android:layout_height="0.75dp"/>
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
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="注册"
/>
android:layout_width="match_parent"
android:layout_height="0.75dp"
android:id="@+id/mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
android:id="@+id/reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>
</LinearLayout>
//content_top LinearLayout android:orientation="horizontal"
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"/>
android:id="@+id/ss_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜做"/>
// item LinearLayout android:orientation="horizontal"
android:id="@+id/item_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/item_sumary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="简介"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="@+id/item_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="原价999"
android:id="@+id/item_price2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="折扣价999"
</LinearLayout>
* 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 code;
private String page;
private List<DataBean> data;
return msg;
}
this.msg = msg;
}
return code;
}
this.code = code;
}
return page;
}
this.page = page;
}
return data;
}
this.data = 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)银
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;
return bargainPrice;
}
this.bargainPrice = bargainPrice;
}
return createtime;
}
this.createtime = createtime;
}
return detailUrl;
}
this.detailUrl = detailUrl;
}
return images;
}
this.images = images;
}
return itemtype;
}
this.itemtype = itemtype;
}
return pid;
}
this.pid = pid;
}
return price;
}
this.price = price;
}
return pscid;
}
this.pscid = pscid;
}
return salenum;
}
this.salenum = salenum;
}
return sellerid;
}
this.sellerid = sellerid;
}
return subhead;
}
this.subhead = subhead;
}
return title;
}
this.title = title;
}
}
}
void getGoodsSuccess(String json);
void getGoodsError(String error);
}
void getGoods(String goodName, String page,GoodsListener goodsListener);
public interface LoginListener {
void loginSucess(String json);
}
import com.gjl.zhoukao2.utils.HttpConig;
import com.gjl.zhoukao2.utils.OkHttpUtils;
import org.json.JSONObject;
@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) {
//登录的接口
JSONObject obj = new JSONObject(json);
String code = obj.getString("code");
if (code.equals("0")) {
loginListener.loginSucess(json);
} else {
loginListener.loginError("失败");
}
e.printStackTrace();
}
public void loadError(String error) {
loginListener.loginError(error);
}
});
//如果成功,
okHttpUtils.okHttpGet(HttpConig.login_url + "?mobile=" + mobile + "&password=" + password);
//失败
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) {
//注册的接口
JSONObject obj = new JSONObject(json);
String code = obj.getString("code");
if (code.equals("0")) {
regListener.regSuccess();
} else {
regListener.regError();
}
e.printStackTrace();
}
public void loadError(String error) {
regListener.regError();
}
});
//如果成功,
okHttpUtils.okHttpGet(HttpConig.reg_url + "?mobile=" + mobile + "&password=" + password);
//失败
}
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);
}
public void loadError(String error) {
goodsListener.getGoodsError(error);
}
});
okHttpUtils.okHttpGet(HttpConig.goods_url + "?keywords=" + goodName + "&page=" + page);
}
}
void loadSuccess(String json);
void loadError(String error);
}
public interface RegListener {
void regSuccess();
void regError();
}
private String mobile;
private String password;
this.mobile = mobile;
this.password = password;
}
return mobile;
}
this.mobile = mobile;
}
return password;
}
this.password = password;
}
public String toString() {
return "User{" +
"mobile='" + mobile + '\'' +
", password='" + password + '\'' +
'}';
}
}
//登录
@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();
}
public void loginError(String error) {
iView.showError();
}
});
}
public void reg(IModel iModel, final IView iView) {
iModel.register(new User(iView.getMobile(), iView.getPassword()), new RegListener() {
public void regSuccess() {
iView.jumpActivity();
}
public void regError() {
iView.showError();
}
});
}
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);
}
public void getGoodsError(String error) {
Toast.makeText(context,"shibai--",Toast.LENGTH_SHORT).show();
iGoodView.showError();
}
});
}
public void changeView(IModel iModel, IGoodView iGoodView) {
//
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";
}
import android.os.Handler;
import android.os.Message;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
private MyHandler myHandler = new MyHandler();
private OkHttpLoadListener okHttpLoadListener;
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);
}
public void onResponse(Call call, Response response) throws IOException {
Message message = myHandler.obtainMessage();
message.what = 1;
message.obj = response.body().string();
myHandler.sendMessage(message);
}
});
}
@Override
public void handleMessage(Message msg) {
int i = msg.what;
switch (i) {
case 0:
String s = (String) msg.obj;
//调用接口
okHttpLoadListener.loadError(s);
break;
String josn = (String) msg.obj;
okHttpLoadListener.loadSuccess(josn);
break;
}
}
}
this.okHttpLoadListener = okHttpLoadListener;
}
}
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class ContentActivity extends AppCompatActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
@Override
public void jump() {
startActivity(new Intent(ContentActivity.this, GoodsActivity.class));
}
});
}
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public FlowLayout(Context context) {
this(context, null);
}
this(context, attrs, 0);
}
super(context, attrs, defStyleAttr);
/**
* 记录每一行的最大高度
*/
private List<Integer> mLineHeight = new ArrayList<Integer>();
protected void onLayout(boolean changed, int l, int t, int r, int b) {
mAllViews.clear();
mLineHeight.clear();
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 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();
int lc = left + lp.leftMargin;
int tc = top + lp.topMargin;
int rc = lc + child.getMeasuredWidth();
int bc = tc + child.getMeasuredHeight();
+ lp.leftMargin;
}
left = 0;
top += lineHeight;
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
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;
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);
}
}
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.gjl.zhoukao2.R;
import com.gjl.zhoukao2.model.Goods;
import com.gjl.zhoukao2.model.ModelImpl;
import com.gjl.zhoukao2.presenter.PresenterImpl;
private ImageView change;
private PresenterImpl presenter;
private ListView listView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goods);
presenter.setData(GoodsActivity.this, new ModelImpl(), this);
recyclerView = findViewById(R.id.recyclerView);
listView = findViewById(R.id.listView);
change = findViewById(R.id.change);
change.setOnClickListener(this);
}
public void setData(Context context, List<Goods.DataBean> list) {
}
public void changeView() {
if (flag) {
change.setImageResource(R.drawable.lv);
change.setImageResource(R.drawable.grid);
recyclerView.setLayoutManager(new LinearLayoutManager(GoodsActivity.this));
}
flag = !flag;
}
public void showError() {
Toast.makeText(GoodsActivity.this, "失败", Toast.LENGTH_SHORT).show();
}
public void onClick(View v) {
presenter.changeView(new ModelImpl(), this);
}
}
import android.content.Context;
void setData(Context context,List<Goods.DataBean> list);
}
String getMobile();
String getPassword();
void showError();
}
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.model.ModelImpl;
import com.gjl.zhoukao2.presenter.PresenterImpl;
private EditText password;
private Button login;
private Button reg;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化界面
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);
}
@Override
public String getMobile() {
return mobile.getText().toString();
}
public String getPassword() {
return password.getText().toString();
}
public void jumpActivity() {
startActivity(new Intent(MainActivity.this, ContentActivity.class));
}
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;
startActivity(new Intent(MainActivity.this,RegActivity.class));
break;
}
}
}
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.gjl.zhoukao2.R;
import com.gjl.zhoukao2.model.Goods;
private final Context context;
this.context = context;
this.list = list;
}
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;
}
public void onBindViewHolder(MyViewHolder holder, int position) {
//
Glide.with(context).load(list.get(position).getImages()).into(holder.getImageView());
}
public int getItemCount() {
return 0;
}
private final TextView sumary;
private final TextView item_price;
private final TextView item_price2;
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);
}
return imageView;
}
return sumary;
}
return item_price;
}
return item_price2;
}
}
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
private MySeacheViewListener mySeacheViewListener;
this(context,null);
}
this(context, attrs,0);
}
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();
}
});
}
void jump();
}
public void setMySeacheViewListener(MySeacheViewListener mySeacheViewListener){
this.mySeacheViewListener = mySeacheViewListener;
}
}
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.model.ModelImpl;
import com.gjl.zhoukao2.presenter.PresenterImpl;
private EditText mobile;
private android.widget.EditText password;
private Button reg;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg);
//初始化界面
initViews();
}
mobile = findViewById(R.id.mobile);
password = findViewById(R.id.password);
reg = findViewById(R.id.reg);
reg.setOnClickListener(this);
}
public void onClick(View v) {
PresenterImpl presenter = new PresenterImpl();
presenter.reg(new ModelImpl(), this);
}
public String getMobile() {
return mobile.getText().toString();
}
public String getPassword() {
return password.getText().toString();
}
public void jumpActivity() {
startActivity(new Intent(RegActivity.this, ContentActivity.class));
}
public void showError() {
Toast.makeText(RegActivity.this, "失败请注册", Toast.LENGTH_SHORT).show();
}
}