package com.bawei.mvp.view;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/25 16:24
*/
public interface MainView {
void sueccss(int type,String data);
void fail(int type,String error);
}
package com.bawei.mvp.model;
import java.util.Map;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/25 16:37
*/
public interface MainModel {
interface CallBackListener {
void success(int type, String data);
void fail(int type, String error);
}
//请求登录
void doLogin(int type, String url, Map<String, String> map, CallBackListener listener);
//获取首页商品
void getShopCar(int type, String url, CallBackListener listener);
//添加购物车
void addShopCar(int type, String url, Map<String, String> headMap, Map<String, String> map, CallBackListener listener);
//获取购物车
void doShopCar(int type, Map<String, String> headMap, String url, CallBackListener listener);
}
package com.bawei.mvp.model;
import com.bawei.mvp.net.HttpListener;
import com.bawei.mvp.net.HttpUtils;
import java.util.Map;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/25 16:37
*/
public class MainModelIml implements MainModel {
//登录
@Override
public void doLogin(final int type, String url, Map<String, String> map, final CallBackListener listener) {
HttpUtils http = new HttpUtils().post(url, map);
result(type, listener, http);
}
//获取首页商品
@Override
public void getShopCar(int type, String url, CallBackListener listener) {
result(type, listener, new HttpUtils().get(url));
}
//添加购物车
@Override
public void addShopCar(int type, String url,Map<String,String> headMap, Map<String, String> map, CallBackListener listener) {
HttpUtils http = new HttpUtils().setHead(headMap).put(url, map);
result(type, listener, http);
}
//获取购物车
@Override
public void doShopCar(int type,Map<String,String> headMap, String url,CallBackListener listener) {
HttpUtils httpUtils = new HttpUtils().setHead(headMap).get(url);
result(type, listener, httpUtils);
}
private void result(final int type, final CallBackListener listener, HttpUtils http) {
http.result(new HttpListener() {
@Override
public void success(String data) {
listener.success(type, data);
}
@Override
public void fail(String error) {
listener.fail(type, error);
}
});
}
}
package com.bawei.mvp.preseter;
import com.bawei.mvp.model.MainModel;
import java.util.Map;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/25 16:27
*/
public interface MainP {
//请求登录
void doLogin(int type, String url, Map<String, String> map);
//获取首页商品
void getShopCar(int type, String url);
//添加购物车
void addShopCar(int type, String url, Map<String, String> headMap, Map<String, String> map);
//获取购物车
void doShopCar(int type, Map<String, String> headMap, String url);
}
package com.bawei.mvp.preseter;
import com.bawei.mvp.model.MainModel;
import com.bawei.mvp.view.MainView;
import java.util.Map;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/25 16:27
*/
public class MainPIml implements MainP, MainModel.CallBackListener {
private MainModel mainModel;
private MainView mainView;
public MainPIml(MainModel mainModel, MainView mainView){
this.mainModel=mainModel;
this.mainView=mainView;
}
@Override
public void doLogin(int type, String url, Map<String, String> map) {
mainModel.doLogin(type,url,map,this);
}
@Override
public void getShopCar(int type, String url) {
mainModel.getShopCar(type,url,this);
}
@Override
public void addShopCar(int type, String url, Map<String, String> headMap, Map<String, String> map) {
mainModel.addShopCar(type,url,headMap,map,this);
}
@Override
public void doShopCar(int type, Map<String, String> headMap, String url) {
mainModel.doShopCar(type,headMap,url,this);
}
@Override
public void success(int type, String data) {
mainView.sueccss(type,data);
}
@Override
public void fail(int type, String error) {
mainView.fail(type,error);
}
public void destroy(){
if (mainModel!=null){
mainModel=null;
}if (mainView!=null){
mainView=null;
}
System.gc();
}
}
net
package com.bawei.mvp.net;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/25 16:37
*/
public interface HttpListener {
void success(String data);
void fail(String error);
}
package com.bawei.mvp.net;
import java.util.Map;
import io.reactivex.Observable;
import okhttp3.ResponseBody;
import retrofit2.http.GET;
import retrofit2.http.HeaderMap;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/25 16:37
*/
public interface HttpService {
@GET
Observable<ResponseBody> get(@Url String url, @HeaderMap Map<String, String> headMap);
@POST
Observable<ResponseBody> post(@Url String url,
@HeaderMap Map<String, String> headMap,
@QueryMap Map<String, String> map);
@PUT
Observable<ResponseBody> put(@Url String url,
@HeaderMap Map<String, String> headMap,
@QueryMap Map<String, String> map);
}
package com.bawei.mvp.net;
import android.os.Environment;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/25 16:37
*/
public class HttpUtils {
private String baseUrl = "http://172.17.8.100";
//用来添加头参
private Map<String, String> headMap = new HashMap<>();
public HttpUtils setHead(Map<String, String> headMap) {
this.headMap = headMap;
return this;
}
public HttpUtils put(String url, Map<String, String> map) {
HttpService service = null;
try {
service = getHttpService();
} catch (IOException e) {
e.printStackTrace();
}
Observable<ResponseBody> ob = service.put(url, headMap, map);
send(ob);
return this;
}
//post请求
public HttpUtils post(String url, Map<String, String> map) {
HttpService service = null;
try {
service = getHttpService();
} catch (IOException e) {
e.printStackTrace();
}
Observable<ResponseBody> ob = service.post(url, headMap, map);
send(ob);
return this;
}
//get请求
public HttpUtils get(String url) {
HttpService service = null;
try {
service = getHttpService();
} catch (IOException e) {
e.printStackTrace();
}
Observable<ResponseBody> ob = service.get(url,headMap);
send(ob);
return this;
}
//订阅
private void send(Observable<ResponseBody> ob) {
ob.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ResponseBody>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseBody responseBody) {
try {
mHttpListener.success(responseBody.string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable e) {
mHttpListener.fail(e.getMessage());
}
@Override
public void onComplete() {
}
});
}
private HttpService getHttpService() throws IOException {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
Cache cache = new Cache(file, 10 * 1024);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
// request.newBuilder().addHeader()
return chain.proceed(request);
}
})
.cache(cache)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
return retrofit.create(HttpService.class);
}
private HttpListener mHttpListener;
public void result(HttpListener mHttpListener) {
this.mHttpListener = mHttpListener;
}
}
fr
package com.bawei.fr;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.bawei.R;
import com.bawei.adapter.ShopCarAdapter;
import com.bawei.bean.CarBean;
import com.bawei.bean.ShopCarBean;
import com.bawei.mvp.model.MainModelIml;
import com.bawei.mvp.preseter.MainPIml;
import com.bawei.mvp.view.MainView;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/26 13:43
*/
public class Fragment1 extends Fragment implements MainView {
private RecyclerView mRecyler;
private ShopCarAdapter shopCarAdapter;
private HashMap headMap;
private MainPIml mainPIml;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=View.inflate(getActivity(), R.layout.fragment1,null);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mRecyler = (RecyclerView) getActivity().findViewById(R.id.recycler);
shopCarAdapter = new ShopCarAdapter(getActivity());
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyler.setLayoutManager(gridLayoutManager);
mRecyler.setAdapter(shopCarAdapter);
headMap = new HashMap<>();
SharedPreferences sp = getActivity().getSharedPreferences("shop", 0);
int userId = sp.getInt("userId", 0);
String sessionId = sp.getString("sessionId", "");
headMap.put("userId", userId + "");
headMap.put("sessionId", sessionId);
mainPIml=new MainPIml(new MainModelIml(),this);
mainPIml.getShopCar(0, "/small/commodity/v1/findCommodityByKeyword?page=1&count=10&keyword=手机");
shopCarAdapter.setOnItemClickListenrer(new ShopCarAdapter.OnItemClickListenrer() {
@Override
public void onItemClick(int id) {
try {
boolean isHave = true;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
int commodityId = json.getInt("commodityId");
int count = json.getInt("count");
if (id == commodityId) {
isHave = false;
count++;
json.put("count", count);
}
}
if (isHave) {//之前购物车里没有
JSONObject jsonObject = new JSONObject();
jsonObject.put("commodityId", id);
jsonObject.put("count", "1");
jsonArray.put(jsonObject);
}
Map<String, String> map = new HashMap<>();
map.put("data", jsonArray.toString());
mainPIml.addShopCar(2, "/small/order/verify/v1/syncShoppingCart", headMap, map);
} catch (Exception e) {
}
}
});
}
private JSONArray jsonArray = new JSONArray();
@Override
public void sueccss(int type, String data) {
if (type == 0) {
ShopCarBean bean = new Gson().fromJson(data, ShopCarBean.class);
shopCarAdapter.setList(bean.getResult());
//去执行添加购物车
mainPIml.doShopCar(1, headMap, "/small/order/verify/v1/findShoppingCart");
} else if (type == 1) {//查询购物车
Log.i("success", data);
CarBean bean = new Gson().fromJson(data, CarBean.class);
try {
for (int i = 0; i < bean.getResult().size(); i++) {
CarBean.ResultBean b = bean.getResult().get(i);
int id = b.getCommodityId();
int count = b.getCount();
JSONObject jsonObject = new JSONObject();
jsonObject.put("commodityId", id);
jsonObject.put("count", count);
jsonArray.put(jsonObject);
}
} catch (Exception e) {
}
} else if (type == 2) {//添加购物车
try {
JSONObject jsonObject=new JSONObject(data);
String status=jsonObject.getString("status");
if(status.equals("0000")){
Toast.makeText(getActivity(),"添加成功",Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.i("success", data);
}
}
@Override
public void fail(int type, String error) {
Log.i("fail", error);
}
}
package com.bawei.fr;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import com.bawei.R;
import com.bawei.adapter.CarAdapter;
import com.bawei.bean.CarBean;
import com.bawei.mvp.model.MainModelIml;
import com.bawei.mvp.preseter.MainPIml;
import com.bawei.mvp.view.MainView;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/26 13:43
*/
public class Fragment2 extends Fragment implements MainView {
private RecyclerView mRecyclerView;
private TextView mAllPrice;
private TextView mAllNum;
private CheckBox allCheck;
private CarAdapter carAdapter;
private MainPIml mainPIml;
private List<CarBean.ResultBean> listBean=new ArrayList<>();
private HashMap<String, String> headMap;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=View.inflate(getActivity(), R.layout.fragment2,null);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.shop_recycler);
mAllPrice = (TextView) getActivity().findViewById(R.id.tv_price);
mAllNum = (TextView) getActivity().findViewById(R.id.tv_num);
//全选
allCheck = (CheckBox) getActivity().findViewById(R.id.checkbox);
carAdapter = new CarAdapter(getActivity());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(carAdapter);
mainPIml=new MainPIml(new MainModelIml(),this);
carAdapter.setOnChangeDataListener(new CarAdapter.OnChangeDataListener() {
@Override
public void changeList(List<CarBean.ResultBean> list) {
Log.i("changeList",list.size()+"");
float priceAll = 0;
int countAll = 0;
int num = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).isCheck()) {//选中
num++;
int price = list.get(i).getPrice();
int count = list.get(i).getCount();
priceAll = priceAll + price * count;//获取总价
countAll = countAll + count;//获取数量
}
}
if (num == list.size()) {//所有的商品都选中
allCheck.setChecked(true);
} else {
allCheck.setChecked(false);
}
mAllPrice.setText(priceAll + "");
mAllNum.setText("去结算(" + countAll + ")");
}
});
getActivity().findViewById(R.id.checkbox).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isCheck = allCheck.isChecked();
float priceAll = 0;
int countAll = 0;
for (int i = 0; i < listBean.size(); i++) {
listBean.get(i).setCheck(isCheck);
int price = listBean.get(i).getPrice();
int count = listBean.get(i).getCount();
priceAll = priceAll + price * count;//获取总价
countAll = countAll + count;//获取数量
}
if (!isCheck) {
priceAll = 0;
countAll = 0;
}
mAllPrice.setText(priceAll + "");
mAllNum.setText("去结算(" + countAll + ")");
carAdapter.setList(listBean);
}
});
}
@Override
public void sueccss(int type, String data) {
CarBean bean = new Gson().fromJson(data, CarBean.class);
if (bean.getStatus().equals("0000")) {//登录成功
listBean = bean.getResult();
carAdapter.setList(listBean);
} else {
Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_LONG).show();
}
}
@Override
public void fail(int type, String error) {
}
//刷新购物车数据
public void notifyData() {
headMap = new HashMap<>();
SharedPreferences sp = getActivity().getSharedPreferences("shop", 0);
int userId = sp.getInt("userId", 0);
String sessionId = sp.getString("sessionId", "");
headMap.put("userId", userId + "");
headMap.put("sessionId", sessionId);
mainPIml.doShopCar(0, headMap, "/small/order/verify/v1/findShoppingCart");
}
}
package com.bawei.fr;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bawei.R;
import com.bawei.bean.UserBean;
import com.bawei.mvp.model.MainModelIml;
import com.bawei.mvp.preseter.MainP;
import com.bawei.mvp.preseter.MainPIml;
import com.bawei.mvp.view.MainView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/26 13:43
*/
public class Fragment3 extends Fragment implements MainView {
private EditText ed_name,ed_pass;
private MainPIml mainPIml;
private RelativeLayout login_success;
private SimpleDraweeView simple;
private TextView tv_nick_name;
private RelativeLayout login_layout;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=View.inflate(getActivity(), R.layout.fragment3,null);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ed_name=(EditText)getActivity().findViewById(R.id.ed_name);
ed_pass=(EditText)getActivity().findViewById(R.id.ed_pass);
login_success=(RelativeLayout) getActivity().findViewById(R.id.login_success);
tv_nick_name=(TextView) getActivity().findViewById(R.id.tv_nick_name);
simple=(SimpleDraweeView) getActivity().findViewById(R.id.simple);
login_layout=(RelativeLayout) getActivity().findViewById(R.id.login_layout);
getActivity().findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doLogin();
}
});
mainPIml=new MainPIml(new MainModelIml(),this);
}
//请求登录方法
private void doLogin() {
String phone = ed_name.getText().toString().trim();
String pass = ed_pass.getText().toString().trim();
if (TextUtils.isEmpty(phone)){
Toast.makeText(getActivity(),"请输入手机号",Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(pass)){
Toast.makeText(getActivity(),"请输密码",Toast.LENGTH_LONG).show();
return;
}
Map<String ,String> map=new HashMap<>();
map.put("phone",phone);
map.put("pwd",pass);
mainPIml.doLogin(0,"/small/user/v1/login",map);
}
@Override
public void sueccss(int type, String data) {
login_success.setVisibility(View.VISIBLE);//显示
login_layout.setVisibility(View.GONE);//隐藏
UserBean bean = new Gson().fromJson(data, UserBean.class);
String headPic = bean.getResult().getHeadPic();
String nickName = bean.getResult().getNickName();
int userId = bean.getResult().getUserId();
String sessionId = bean.getResult().getSessionId();
tv_nick_name.setText(nickName);
simple.setImageURI(headPic);//用户头像
//保存数据
SharedPreferences sp = getActivity().getSharedPreferences("shop", 0);
sp.edit().putString("headPic",headPic)
.putString("nickName",nickName)
.putString("sessionId",sessionId)
.putInt("userId",userId).commit();
}
@Override
public void fail(int type, String error) {
}
}
package com.bawei.bean;
import java.util.List;
/**
* 购物车的bean
*/
public class CarBean {
/**
* result : [{"commodityId":27,"commodityName":"休闲马衔扣保暖绒里棉鞋懒人鞋毛毛鞋平底女雪地靴女短靴子豆豆鞋女鞋","count":2,"pic":"http://172.17.8.100/images/small/commodity/nx/ddx/3/1.jpg","price":139}]
* message : 查询成功
* status : 0000
*/
private String message;
private String status;
private List<ResultBean> result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean {
/**
* commodityId : 27
* commodityName : 休闲马衔扣保暖绒里棉鞋懒人鞋毛毛鞋平底女雪地靴女短靴子豆豆鞋女鞋
* count : 2
* pic : http://172.17.8.100/images/small/commodity/nx/ddx/3/1.jpg
* price : 139
*/
private int commodityId;
private String commodityName;
private int count;
private String pic;
private int price;
private boolean check;
public boolean isCheck() {
return check;
}
public void setCheck(boolean check) {
this.check = check;
}
public int getCommodityId() {
return commodityId;
}
public void setCommodityId(int commodityId) {
this.commodityId = commodityId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
}
package com.bawei.bean;
import java.util.List;
public class ShopCarBean {
/**
* result : [{"commodityId":186,"commodityName":"SWISSGEAR双肩包 防水多功能笔记本电脑包14.6英寸/15.6英寸商务背包男学生书包休闲 SA-9393III","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/sjb/3/1.jpg","price":99,"saleNum":0},{"commodityId":88,"commodityName":"冬新品 梵希蔓毛呢外套女短款长袖2018冬季新款宽松英伦格子翻领斜门襟大衣女","masterPic":"http://172.17.8.100/images/small/commodity/nz/wt/3/1.jpg","price":358,"saleNum":0},{"commodityId":124,"commodityName":"台湾AC5高清4K摄像机数码DV12倍光学变焦专业家用旅游","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/zxj/4/1.jpg","price":1998,"saleNum":0},{"commodityId":171,"commodityName":"HOTBOOM2018男士双肩包休闲背包潮流学生书包多功能笔记本商务14英寸电脑包5606 时尚灰","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/dnb/2/1.jpg","price":109,"saleNum":0},{"commodityId":118,"commodityName":" 新款 iPad 128G WIFI 版 平板电脑","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/yyyl/5/1.jpg","price":2988,"saleNum":0},{"commodityId":132,"commodityName":"10升不锈钢智能垃圾桶 时尚香槟金感应收纳桶 家用酒店用厨房环保桶","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/znsb/5/1.jpg","price":298,"saleNum":0},{"commodityId":179,"commodityName":"莎米特SUMMIT拉杆箱22英寸PC材质万向轮旅行箱行李箱PC154T4A可扩容","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/lgx/3/1.jpg","price":199,"saleNum":0},{"commodityId":190,"commodityName":"XDDESIGN 蒙马特城市安全防盗背包 经典版 商务男女款双肩包15英寸笔记本电脑包 ","masterPic":"http://172.17.8.100/images/small/commodity/xbsd/sjb/7/1.jpg","price":369,"saleNum":0},{"commodityId":106,"commodityName":"三星Galaxy S9+ 6GB+128GB版曲屏手机/指纹识别手机/拍照手机","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/sj/7/1.jpg","price":6199,"saleNum":0},{"commodityId":127,"commodityName":"【夜视高清 智能摄像头】小米语音互动 1080P 夜视高清 智能摄像头","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/zxj/7/1.jpg","price":399,"saleNum":0}]
* message : 查询成功
* status : 0000
*/
private String message;
private String status;
private List<ResultBean> result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean {
/**
* commodityId : 186
* commodityName : SWISSGEAR双肩包 防水多功能笔记本电脑包14.6英寸/15.6英寸商务背包男学生书包休闲 SA-9393III
* masterPic : http://172.17.8.100/images/small/commodity/xbsd/sjb/3/1.jpg
* price : 99
* saleNum : 0
*/
private int commodityId;
private String commodityName;
private String masterPic;
private int price;
private int saleNum;
public int getCommodityId() {
return commodityId;
}
public void setCommodityId(int commodityId) {
this.commodityId = commodityId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getMasterPic() {
return masterPic;
}
public void setMasterPic(String masterPic) {
this.masterPic = masterPic;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSaleNum() {
return saleNum;
}
public void setSaleNum(int saleNum) {
this.saleNum = saleNum;
}
}
}
package com.bawei.bean;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/26 19:46
*/
public class UserBean {
/**
* result : {"headPic":"http://172.17.8.100/images/small/head_pic/2018-11-21/20181121100733.jpg","nickName":"OP_8mY65","phone":"16619958760","sessionId":"154276714558512","sex":1,"userId":12}
* message : 登录成功
* status : 0000
*/
private ResultBean result;
private String message;
private String status;
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public static class ResultBean {
/**
* headPic : http://172.17.8.100/images/small/head_pic/2018-11-21/20181121100733.jpg
* nickName : OP_8mY65
* phone : 16619958760
* sessionId : 154276714558512
* sex : 1
* userId : 12
*/
private String headPic;
private String nickName;
private String phone;
private String sessionId;
private int sex;
private int userId;
public String getHeadPic() {
return headPic;
}
public void setHeadPic(String headPic) {
this.headPic = headPic;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}
}
package com.bawei.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import com.bawei.R;
import com.bawei.ShopCarAddView;
import com.bawei.bean.CarBean;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/25 16:37
* 购物车适配器
*/
public class CarAdapter extends RecyclerView.Adapter<CarAdapter.CarViewHolder> {
private List<CarBean.ResultBean> list = new ArrayList<>();
private Context context;
public CarAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public CarViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.car_item, null);
CarViewHolder carViewHolder = new CarViewHolder(view);
return carViewHolder;
}
@Override
public void onBindViewHolder(@NonNull final CarViewHolder carViewHolder, final int i) {
carViewHolder.mTextView.setText(list.get(i).getCommodityName());
carViewHolder.mSimple.setImageURI(list.get(i).getPic());
carViewHolder.mPrice.setText(list.get(i).getPrice() + "");
carViewHolder.mShopCarView.setNum(list.get(i).getCount());
carViewHolder.mCheck.setChecked(list.get(i).isCheck());
carViewHolder.mCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean ischeck=carViewHolder.mCheck.isChecked();
list.get(i).setCheck(ischeck);
mOnChangeDataListener.changeList(list);
}
});
carViewHolder.mShopCarView.setChangeNumListener(new ShopCarAddView.ChangeNumListener() {
@Override
public void num(int num) {
list.get(i).setCount(num);
mOnChangeDataListener.changeList(list);
}
});
}
@Override
public int getItemCount() {
if (list == null) {
return 0;
}
return list.size();
}
public void setList(List<CarBean.ResultBean> list) {
this.list = list;
notifyDataSetChanged();
}
public class CarViewHolder extends RecyclerView.ViewHolder {
ShopCarAddView mShopCarView;
TextView mTextView, mPrice;
SimpleDraweeView mSimple;
CheckBox mCheck;
public CarViewHolder(@NonNull View itemView) {
super(itemView);
mCheck = (CheckBox) itemView.findViewById(R.id.checkbox);
mSimple = (SimpleDraweeView) itemView.findViewById(R.id.simple);
mTextView = (TextView) itemView.findViewById(R.id.tv_title);
mPrice = (TextView) itemView.findViewById(R.id.tv_price);
mShopCarView = (ShopCarAddView) itemView.findViewById(R.id.carview);
}
}
private OnChangeDataListener mOnChangeDataListener;
public void setOnChangeDataListener(OnChangeDataListener mOnChangeDataListener) {
this.mOnChangeDataListener = mOnChangeDataListener;
}
public interface OnChangeDataListener {
void changeList(List<CarBean.ResultBean> list);
}
}
package com.bawei.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.bawei.R;
import com.bawei.bean.ShopCarBean;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/25 16:37
*/
public class ShopCarAdapter extends RecyclerView.Adapter<ShopCarAdapter.ShopCarViewHolder> {
private List<ShopCarBean.ResultBean> list = new ArrayList<>();
private Context context;
public ShopCarAdapter(Context context) {
thi.s.context = context;
}
@NonNull
@Override
public ShopCarViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.shop_car_adapter, null);
ShopCarViewHolder shopCarViewHolder = new ShopCarViewHolder(view);
return shopCarViewHolder;
}
@Override
public void onBindViewHolder(@NonNull ShopCarViewHolder shopCarViewHolder, final int i) {
shopCarViewHolder.mSimple.setImageURI(list.get(i).getMasterPic());
shopCarViewHolder.mTitle.setText(list.get(i).getCommodityName());
shopCarViewHolder.mPrice.setText(list.get(i).getPrice() + "");
shopCarViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListenrer.onItemClick(list.get(i).getCommodityId());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public void setList(List<ShopCarBean.ResultBean> list) {
this.list = list;
notifyDataSetChanged();
}
public class ShopCarViewHolder extends RecyclerView.ViewHolder {
TextView mTitle, mPrice;
SimpleDraweeView mSimple;
public ShopCarViewHolder(@NonNull View itemView) {
super(itemView);
mSimple = (SimpleDraweeView) itemView.findViewById(R.id.simple);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mPrice = (TextView) itemView.findViewById(R.id.tv_price);
}
}
private OnItemClickListenrer mOnItemClickListenrer;
public void setOnItemClickListenrer(OnItemClickListenrer mOnItemClickListenrer){
this.mOnItemClickListenrer=mOnItemClickListenrer;
}
public interface OnItemClickListenrer{
void onItemClick(int i);
}
}
package com.bawei;
import android.app.Application;
import android.os.Environment;
import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import java.io.File;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/25 16:37
*/
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this, ImagePipelineConfig.newBuilder(App.this)
.setMainDiskCacheConfig(
DiskCacheConfig.newBuilder(this)
//磁盘缓存路径
.setBaseDirectoryPath(new File(Environment.getExternalStorageDirectory().getAbsolutePath())) // 注意Android运行时权限。
.setMaxCacheSize(10 * 1024 * 1024)
.build()
)
.build()
);
}
}
package com.bawei;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.bawei.fr.Fragment1;
import com.bawei.fr.Fragment2;
import com.bawei.fr.Fragment3;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager viewpagr;
private TabLayout tabel;
private FragmentAdapter fragmenAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpagr=(ViewPager) findViewById(R.id.viewpagr);
tabel=(TabLayout)findViewById(R.id.tabel);
fragmenAdapter=new FragmentAdapter (getSupportFragmentManager());
viewpagr.setAdapter(fragmenAdapter);
tabel.setupWithViewPager(viewpagr);
viewpagr.setOffscreenPageLimit(3);
viewpagr.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
if (i == 1) {//购物车页面
/* Fragment fragment = fragmentAdapter.getItem(i);
if (fragment instanceof ShopCarFragment) {
((ShopCarFragment) fragment).notifyData();
}*/
Fragment fragment = fragmenAdapter.getItem(i);
if (fragment instanceof Fragment2){
((Fragment2)fragment).notifyData();
}
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
private class FragmentAdapter extends FragmentPagerAdapter {
private String[] mTitle = {
"首页", "购物车","我的"
};
private List<Fragment> fragmentList=new ArrayList<>();
public FragmentAdapter(FragmentManager fm) {
super(fm);
fragmentList.add(new Fragment1());
fragmentList.add(new Fragment2());
fragmentList.add(new Fragment3());
}
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mTitle[position];
}
}
}
package com.bawei;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
/**
* author:AbnerMing
* date:2019/4/26
*/
public class ShopCarAddView extends RelativeLayout implements View.OnClickListener {
private EditText mEdText;
public ShopCarAddView(Context context) {
super(context);
init(context);
}
public ShopCarAddView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private Context mContext;
private void init(Context context) {
this.mContext = context;
View view = View.inflate(context, R.layout.shop_car_add, null);
mEdText = (EditText) view.findViewById(R.id.ed_text);
view.findViewById(R.id.tv_jian).setOnClickListener(this);
view.findViewById(R.id.tv_jia).setOnClickListener(this);
addView(view);
}
public void setNum(int num) {
mEdText.setText(num + "");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_jia://加
String num = mEdText.getText().toString().trim();
int n = Integer.parseInt(num);
n++;
mChangeNumListener.num(n);
mEdText.setText(n + "");
break;
case R.id.tv_jian://减
String numJian = mEdText.getText().toString().trim();
int nJian = Integer.parseInt(numJian);
if (nJian == 1) {
Toast.makeText(mContext, "至少得有一个商品哦~", Toast.LENGTH_LONG).show();
return;
}
nJian--;
mChangeNumListener.num(nJian);
mEdText.setText(nJian + "");
break;
}
}
private ChangeNumListener mChangeNumListener;
public void setChangeNumListener(ChangeNumListener mChangeNumListener) {
this.mChangeNumListener = mChangeNumListener;
}
public interface ChangeNumListener {
void num(int num);
}
}
<?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"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_above="@+id/tabel"
android:id="@+id/viewpagr"/>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/tabel"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<?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"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="10dp">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/simple"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_toRightOf="@+id/checkbox" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/simple">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#d43c3c" />
<!--自定义的加减器-->
<com.bawei.ShopCarAddView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/carview"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>``
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:id="@+id/recycler"
/>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:id="@+id/shop_recycler"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_above="@+id/layout" />
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="全选" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/checkbox"
android:text="0.0"
android:textColor="#d43c3c" />
<RelativeLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="#d43c3c">
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="去结算(0)"
android:textColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<!--登录过后展示的UI-->
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/login_success"
android:background="#ffffff"
android:visibility="gone">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_height="100dp"
android:layout_width="100dp"
android:id="@+id/simple"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tv_nick_name"
android:layout_marginTop="20dp"
android:layout_below="@+id/simple"
android:layout_centerHorizontal="true"/>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/simple"
android:layout_width="100dp"
android:layout_height="80dp" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/simple" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_title"
android:textColor="#d43c3c" />
</RelativeLayout>
```
<?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"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_jian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="25sp"
android:padding="5dp"
/>
<EditText
android:id="@+id/ed_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_toRightOf="@+id/tv_jian"
android:background="@drawable/bg"
android:gravity="center"
android:inputType="number"
/>
<TextView
android:id="@+id/tv_jia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ed_text"
android:text="+"
android:textSize="25sp"
android:padding="5dp"
/>
</RelativeLayout>
</RelativeLayout>