(购物车单独的在下一篇博客)
首先是依赖
//image-loader
implementation files('libs/universal-image-loader-1.9.5.jar')
//gson
implementation files('libs/gson-2.5.jar')
// Glide加载图片
compile 'com.github.bumptech.glide:glide:3.7.0'
// recyclerview
implementation 'com.android.support:recyclerview-v7:26.1.0'
/*okhttp网络请求*/
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
/*轮播图 依赖*/
implementation 'com.youth.banner:banner:1.4.9'
权限
<uses-permission android:name="android.permission.INTERNET" />
主页面布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bb"
tools:context="com.bwei.monthtext111218.MainActivity">
<ImageView
android:id="@+id/main_img"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_above="@+id/mian_btn"
android:layout_alignEnd="@+id/mian_btn"
android:layout_alignRight="@+id/mian_btn"
android:src="@drawable/qq" />
<Button
android:id="@+id/mian_btn"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp"
android:background="#c6daf0"
android:text="开始旅程" />
</RelativeLayout>
第二个activity的布局
activity_secend.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bwei.monthtext111218.SecendActivity">
<android.support.v4.view.ViewPager
android:layout_weight="1"
android:id="@+id/secend_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:orientation="horizontal"
android:layout_weight="9"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:orientation="horizontal"
android:id="@+id/secend_radio_group"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/rb1"
android:text="首页"
android:textSize="20sp"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/rb2"
android:text="我的"
android:textSize="20sp"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
第一个fragment的布局
f1_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/shape"/>
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="200dp">
</com.youth.banner.Banner>
<android.support.v7.widget.RecyclerView
android:id="@+id/scend_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
瀑布流布局
staggerred_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/img"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text="模拟数据"
android:textSize="25sp"
android:paddingTop="30dp"/>
</LinearLayout>
基类(base)
BasePresenter
public abstract class BasePresenter<V extends IView> {
protected V view;
public BasePresenter(V view){
this.view=view;
initModel();
}
protected abstract void initModel();
void onDestroy(){
view=null;
}
}
IView
public interface IView {
Context context();
}
关于数据的类
PicBean(轮播图)
public class PicBean {
/**
* msg :
* code : 0
* data : [{"aid":1,"createtime":"2018-12-17T14:53:49","icon":"http://www.zhaoapi.cn/images/quarter/ad1.png","productId":null,"title":"第十三界瑞丽模特大赛","type":0,"url":"http://m.mv14449315.icoc.bz/index.jsp"},{"aid":2,"createtime":"2018-12-17T14:53:49","icon":"http://www.zhaoapi.cn/images/quarter/ad2.png","productId":null,"title":"文化艺术节","type":0,"url":"http://m.mv14449315.icoc.bz/index.jsp"},{"aid":3,"createtime":"2018-12-17T14:53:49","icon":"http://www.zhaoapi.cn/images/quarter/ad3.png","productId":null,"title":"直播封面标准","type":0,"url":"http://m.mv14449315.icoc.bz/index.jsp"},{"aid":4,"createtime":"2018-12-17T14:53:49","icon":"http://www.zhaoapi.cn/images/quarter/ad4.png","productId":"1","title":"人气谁最高,金主谁最豪气","type":1,"url":""}]
*/
private String msg;
private String code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* aid : 1
* createtime : 2018-12-17T14:53:49
* icon : http://www.zhaoapi.cn/images/quarter/ad1.png
* productId : null
* title : 第十三界瑞丽模特大赛
* type : 0
* url : http://m.mv14449315.icoc.bz/index.jsp
*/
private int aid;
private String createtime;
private String icon;
private Object productId;
private String title;
private int type;
private String url;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public Object getProductId() {
return productId;
}
public void setProductId(Object productId) {
this.productId = productId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
StaggeredBean(瀑布流)
public class StaggeredBean {
/**
* 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.0
* 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.0
* 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 double bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int itemtype;
private int pid;
private double price;
private int pscid;
private int salenum;
private int sellerid;
private String subhead;
private String title;
public double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double 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 double getPrice() {
return price;
}
public void setPrice(double 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;
}
}
}
主页面
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView main_img;
/**
* 开始旅程
*/
private Button mian_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void jumpPage() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//跳转页面
Intent intent = new Intent(MainActivity.this, SecendActivity.class);
startActivity(intent);
}
},4000);
}
private void initView() {
main_img = (ImageView) findViewById(R.id.main_img);
mian_btn = (Button) findViewById(R.id.mian_btn);
mian_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.mian_btn:
//旋转180度
ObjectAnimator ani1 = ObjectAnimator.ofFloat(main_img, "rotation", 0f,-180f);
//透明度变换
ObjectAnimator ani2 = ObjectAnimator.ofFloat(main_img, "alpha", 1f,0f,0.8f);
AnimatorSet as = new AnimatorSet();
as.playTogether(ani1,ani2);
as.setDuration(3000);
as.start();
jumpPage();
break;
}
}
}
第二个主页面
SecendActivity
public class SecendActivity extends AppCompatActivity {
private ViewPager secend_view_pager;
/**
* 首页
*/
private RadioButton rb1;
/**
* 我的
*/
private RadioButton rb2;
private RadioGroup secend_radio_group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secend);
initView();
List<Fragment> list = new ArrayList<>();
list.add(new F1Fragment());
list.add(new F2Fragment());
FragmentMyAdapter myAdapter = new FragmentMyAdapter(getSupportFragmentManager(), list);
secend_view_pager.setAdapter(myAdapter);
//radiogroup点击按钮切换页面viewpager
secend_radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rb1:
secend_view_pager.setCurrentItem(0);
break;
case R.id.rb2:
secend_view_pager.setCurrentItem(1);
break;
}
}
});
}
private void initView() {
secend_view_pager = findViewById(R.id.secend_view_pager);
rb1 = findViewById(R.id.rb1);
rb2 = findViewById(R.id.rb2);
secend_radio_group = findViewById(R.id.secend_radio_group);
}
}
fragment里面的类
基类(BaseFragment)
public abstract class BaseFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return oncreateView(inflater, container, savedInstanceState);
}
protected abstract View oncreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser){
getDataFromNet();
}
}
protected abstract void getDataFromNet();
}
F1Fragment
public class F1Fragment extends BaseFragment implements MainView{
private View view;
private Banner banner;
private RecyclerView scend_recycler;
private MainPresenter mainPresenter=new MainPresenter(this);
@Override
protected View oncreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.f1_layout, null);
initView();
return view;
}
private void initView() {
banner = view.findViewById(R.id.banner);
scend_recycler = view.findViewById(R.id.scend_recycler);
}
private void Pic() {
//ok网络请求
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.zhaoapi.cn/ad/getAd")
.build();
Call call = okHttpClient.newCall(request);
//用okHttp里面的call对象打点调用 异步请求数据的抽象方法
call.enqueue(new Callback() {
//建个集合 用来存放图片的url地址
private List<String> picUrlList;
//此集合是bean解析过来的集合
private List<PicBean.DataBean> list;
@Override
public void onFailure(Call call, IOException e) {
//访问网络失败的方法(自动生成的)
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//访问网络成功的方法(自动生成的)
//这是bena里边的数据
String json = response.body().string();
Log.e("mkk",json);
//开始Gson解析
Gson gson = new Gson();
PicBean myBean = gson.fromJson(json, PicBean.class);
//拿到bean类里的集合
list = myBean.getData();
//设此局此集合专门用来存放图片url地址的
picUrlList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
//循环吧图片地址添加到String泛型的集合里
picUrlList.add(list.get(i).getIcon());
}
//子线程不能更新UI
//所以【必须】开启返回主线程的方法
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//吧更新UI视图的代码放这里面
//把更新主UI视图的代码放这里面
// .setImagees(把图片地址集合)
// .setImageLoader(new你的Glid类)
// .start开始装逼吧
banner.setImages(picUrlList).setImageLoader(new GlidApplication()).start();
}
});
}
});
}
@Override
protected void getDataFromNet() {
//瀑布流
mainPresenter.getNews();
//轮播图
Pic();
}
@Override
public Context context() {
return getContext();
}
@Override
public void onSuccess(StaggeredBean staggeredBean) {
List<StaggeredBean.DataBean> list = staggeredBean.getData();
//瀑布式布局管理
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
scend_recycler.setLayoutManager(staggeredGridLayoutManager);
//适配器
RecycleViewAdapter mAdapter = new RecycleViewAdapter(list, getActivity());
scend_recycler.setAdapter(mAdapter);
mAdapter.setmkkListener(new RecycleViewAdapter.mkkListener() {
@Override
public void success(String data) {
Toast.makeText(getActivity(), data, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onFaild(String error) {
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
F2Fragment
(购物车)
glid
GlidApplication
(自定义的类 用来轮播图片)
//注意:
import com.youth.banner.loader.ImageLoader;
public class GlidApplication extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
//初始化Glid包
Glide.with(context).load(path).into(imageView);
}
}
adapter
(fragment的适配器)
FragmentMyAdapter
public class FragmentMyAdapter extends FragmentPagerAdapter {
private List<Fragment> list;
public FragmentMyAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
this.list = list;
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
(瀑布流的适配器)
RecycleViewAdapter
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> {
private List<StaggeredBean.DataBean> mList;
private Context mContext;
public RecycleViewAdapter(List<StaggeredBean.DataBean> list, Context context) {
mList = list;
mContext = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(mContext, R.layout.staggerred_item,null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder,final int i) {
viewHolder.mTv.setText(mList.get(i).getTitle());
String images = mList.get(i).getImages();//得到图片集
String[] split = images.split("\\|");//得到一个图片
if (split.length>0) {
//将https成http 进行联网显示
String replace = split[0].replace("https", "http");
Glide.with(mContext).load(replace).into(viewHolder.mImg);//设置图片
}
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mkkListener.success(mList.get(i).getTitle());
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final ImageView mImg;
private final TextView mTv;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mImg = itemView.findViewById(R.id.img);
mTv = itemView.findViewById(R.id.tv);
}
}
public interface mkkListener{
void success(String data);
}
private mkkListener mkkListener;
public void setmkkListener(mkkListener mkkListeners){
mkkListener = mkkListeners;
}
}
MVP
HttpConfig
public class HttpConfig {
public static final String url = "http://www.zhaoapi.cn/product/searchProducts?keywords=笔记本&page=1";
}
OkHttpUtils
public class OkHttpUtils {
public static OkHttpUtils oKhttpUtils;
private final OkHttpClient okHttpClient;
private final Handler myhandler;
private OkHttpUtils() {
//日志拦截器
// HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
// httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//主线程Handler
myhandler = new Handler(Looper.getMainLooper());
okHttpClient = new OkHttpClient.Builder()
.readTimeout(5000, TimeUnit.MILLISECONDS)
.writeTimeout(5000, TimeUnit.MILLISECONDS)
.connectTimeout(5000, TimeUnit.MILLISECONDS)
//内存泄漏
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
// .addInterceptor(httpLoggingInterceptor)
.build();
}
public static OkHttpUtils getoKhttpUtils() {
if (oKhttpUtils == null) {
synchronized (OkHttpUtils.class) {
if (oKhttpUtils == null) {
return oKhttpUtils = new OkHttpUtils();
}
}
}
return oKhttpUtils;
}
//异步get
public void doGet(String url, final IOKhttpUtilsCallback ioKhttpUtilsCallback) {
Request request = new Request.Builder()
.get()
.url(url)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
if (ioKhttpUtilsCallback != null) {
//切换到主线程
myhandler.post(new Runnable() {
@Override
public void run() {
ioKhttpUtilsCallback.onFailure(e.getMessage());
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response != null && response.isSuccessful()) {
final String json = response.body().string();
if (ioKhttpUtilsCallback != null) {
//切换到主线程
myhandler.post(new Runnable() {
@Override
public void run() {
ioKhttpUtilsCallback.onResponse(json);
}
});
}
} else {
if (ioKhttpUtilsCallback != null) {
//切换到主线程
myhandler.post(new Runnable() {
@Override
public void run() {
ioKhttpUtilsCallback.onFailure("网络异常");
}
});
}
}
}
});
}
//异步post
public void doPost(String url, Map<String, String> map, final IOKhttpUtilsCallback ioKhttpUtilsCallback) {
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry<String, String> entry : map.entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
FormBody formBody = builder.build();
Request request = new Request.Builder()
.post(formBody)
.url(url)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
if (ioKhttpUtilsCallback != null) {
//切换到主线程
myhandler.post(new Runnable() {
@Override
public void run() {
ioKhttpUtilsCallback.onFailure(e.getMessage());
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response != null && response.isSuccessful()) {
final String json = response.body().string();
if (ioKhttpUtilsCallback != null) {
//切换到主线程
myhandler.post(new Runnable() {
@Override
public void run() {
ioKhttpUtilsCallback.onResponse(json);
}
});
}
} else {
if (ioKhttpUtilsCallback != null) {
//切换到主线程
myhandler.post(new Runnable() {
@Override
public void run() {
ioKhttpUtilsCallback.onFailure("网络异常");
}
});
}
}
}
});
}
//接口回调
public interface IOKhttpUtilsCallback {
void onFailure(String error);
void onResponse(String json);
}
}
MainModel
public class MainModel {
private static final String TAG = "MainModel_______";
public void getNews(final IMainModelCallback iMainModelCallback){
OkHttpUtils oKhttpUtils = OkHttpUtils.getoKhttpUtils();
oKhttpUtils.doGet(HttpConfig.url , new OkHttpUtils.IOKhttpUtilsCallback() {
@Override
public void onFailure(String error) {
if (iMainModelCallback!=null) {
iMainModelCallback.getFaild(error);
}
}
@Override
public void onResponse(String json) {
StaggeredBean staggeredBean = new Gson().fromJson(json, StaggeredBean.class);
String code = staggeredBean.getCode();
if(code.equals("0")){
if (iMainModelCallback!=null) {
Log.d(TAG, "onResponse: ____________"+staggeredBean.getData().toString());
iMainModelCallback.getSuccess(staggeredBean);
}
}else{
if (iMainModelCallback!=null) {
iMainModelCallback.getFaild("请求数据失败");
}
}
}
});
}
public interface IMainModelCallback{
void getSuccess(StaggeredBean staggeredBean);
void getFaild(String error);
}
}
MainPresenter
public class MainPresenter extends BasePresenter<MainView> {
private static final String TAG = "MainPresenter+++++++++";
private MainModel mainModel;
public MainPresenter(MainView iMainView) {
super(iMainView);
}
@Override
protected void initModel() {
mainModel = new MainModel();
}
public void getNews() {
mainModel.getNews(new MainModel.IMainModelCallback() {
@Override
public void getSuccess(StaggeredBean staggeredBean) {
if (view != null) {
Log.d(TAG, "getSuccess: ++++++++++++++"+staggeredBean.getData().toString());
view.onSuccess(staggeredBean);
}
}
@Override
public void getFaild(String error) {
if (view != null) {
view.onFaild(error);
}
}
});
}
}
MainView
public interface MainView extends IView {
void onSuccess(StaggeredBean staggeredBean);
void onFaild(String error);
}