项目结构
HttpConfig
public class HttpConfig {
public static String goodsDetail = “https://www.zhaoapi.cn/product/getProductDetail“;
public static String goodsAddToCar = “https://www.zhaoapi.cn/product/addCart“;
public static String goodsCar = “https://www.zhaoapi.cn/product/getCarts“;
}
OkHttpUtil
封装代码
OkLoadListener
public interface OkLoadListener {
void okLoadSuccess(String json);
void okLoadError(String error);
}
AddCarListener
public interface AddCarListener {
void setAddCarSuccessListener(String json);
void setAddCarErrorListener(String error);
}
CarLoadListener
public interface CarLoadListener {
//购物车加载成功
void loadCarSuccess(String json);
//购物车加载失败
void loadCarError(String error);
}
DetailLoadListener
public interface DetailLoadListener {
void detailLoadSuccess(String json);
void detailLoadError(String error);
}
IModel
public interface IModel {
//请求接口,得到详情页数据
void getDetailData(String url, Map
ModelImpl
public class ModelImpl implements IModel {
private static final String TAG = “ModelImpl”;
@Override
public void getDetailData(String url, Map<String, String> param, final DetailLoadListener detailLoadListener) {
OkHttpUtil okHttpUtil = new OkHttpUtil();
okHttpUtil.okPost(url, param);
okHttpUtil.setOkLoadListener(new OkLoadListener() {
@Override
public void okLoadSuccess(String json) {
Log.d(TAG, "okLoadSuccess: ----" + json);
detailLoadListener.detailLoadSuccess(json);
}
@Override
public void okLoadError(String error) {
detailLoadListener.detailLoadError(error);
}
});
}
@Override
public void getCarDatas(String url, Map<String, String> param, final CarLoadListener carLoadListener) {
OkHttpUtil okHttpUtil = new OkHttpUtil();
okHttpUtil.okPost(url, param);
okHttpUtil.setOkLoadListener(new OkLoadListener() {
@Override
public void okLoadSuccess(String json) {
carLoadListener.loadCarSuccess(json);
}
@Override
public void okLoadError(String error) {
carLoadListener.loadCarError(error);
}
});
}
@Override
public double calculate(GoodCar goodCar) {
double sum = 0;
List<GoodCar.DataBean> data = goodCar.getData();
for (int i = 0; i < data.size(); i++) {
List<GoodCar.DataBean.ListBean> list = data.get(i).getList();
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getChild_flag()) {
double bargainPrice = list.get(j).getBargainPrice();
sum += bargainPrice;
}
}
}
Log.d(TAG, "calculate: -----" + sum);
return sum;
}
//添加购物车
@Override
public void getAddCarData(String url, Map<String, String> param, final AddCarListener addCarListener) {
OkHttpUtil okHttpUtil = OkHttpUtil.getInstance();
okHttpUtil.okPost(url, param);
okHttpUtil.setOkLoadListener(new OkLoadListener() {
@Override
public void okLoadSuccess(String json) {
addCarListener.setAddCarSuccessListener(json);
}
@Override
public void okLoadError(String error) {
addCarListener.setAddCarErrorListener(error);
}
});
}
}
IPresenter
public interface IPresenter {
//将数据显示Detail页面
void showDataToDetail(IModel iModel, IMainView iMainView);
//将数据显示到购物车中
void showDataToCar(Context context, IModel iModel, ICarView iCarView);
//总价显示到购物车
void showGoodSum(IModel iModel, GoodCar goodCar, ICarView iCarView);
//将详情页面的商品展示到购物车中
void showAddGoodToCar(IModel iModel, IMainView iMainView);
}
PresenterImpl
public class PresenterImpl implements IPresenter {
private static final String TAG = “PresenterImpl”;
@Override
public void showDataToDetail(IModel iModel, final IMainView iMainView) {
HashMap<String, String> map = new HashMap<>();
map.put("pid", "17");
iModel.getDetailData(HttpConfig.goodsDetail, map, new DetailLoadListener() {
@Override
public void detailLoadSuccess(String json) {
Log.d(TAG, "detailLoadSuccess:----- " + json);
Gson gson = new Gson();
GoodDetail goodDetail = gson.fromJson(json, GoodDetail.class);
//传入View
iMainView.showDetailData(goodDetail);
}
@Override
public void detailLoadError(String error) {
Log.d(TAG, "detailLoadError: ------" + error);
}
});
}
@Override
public void showDataToCar(final Context context, IModel iModel, final ICarView iCarView) {
HashMap<String, String> map = new HashMap<>();
map.put("uid", "71");
iModel.getCarDatas(HttpConfig.goodsCar, map, new CarLoadListener() {
@Override
public void loadCarSuccess(String json) {
Log.d(TAG, "loadCarSuccess:---- " + json);
Gson gson = new Gson();
GoodCar goodCar = gson.fromJson(json, GoodCar.class);
//将数据传到CarView展示
iCarView.showDataToCar(context, goodCar);
}
@Override
public void loadCarError(String error) {
Log.d(TAG, "loadCarError: ----" + error);
}
});
}
@Override
public void showGoodSum(IModel iModel, GoodCar goodCar, ICarView iCarView) {
double sum = iModel.calculate(goodCar);
int sum1 = (int) sum;
Log.d(TAG, "showGoodSum: -----" + sum);
iCarView.goodSum(sum1);
}
@Override
public void showAddGoodToCar(IModel iModel, final IMainView iMainView) {
HashMap<String, String> map = new HashMap<>();
map.put("uid", "71");
int pid = (int)iMainView.getPid();
map.put("pid","17");
iModel.getAddCarData(HttpConfig.goodsAddToCar, map, new AddCarListener() {
@Override
public void setAddCarSuccessListener(String json) {
Log.d(TAG, "setAddCarSuccessListener: ----" + json);
try {
JSONObject jsonObject = new JSONObject(json);
String code = jsonObject.getString("code");
if (code.equals("0")) {
iMainView.showAddSuccess();
} else {
iMainView.showAddError();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void setAddCarErrorListener(String error) {
Log.d(TAG, "setAddCarErrorListener: ----" + error);
iMainView.showAddError();
}
});
}
}
AnimatorUtil
public class AnimatorUtil {
public static AnimatorSet setAnimatorSet(Context context, View view) {
//获取当前窗口大小
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int height = displayMetrics.heightPixels;
ObjectAnimator translationY = ObjectAnimator.ofFloat(view, “translationY”, 0, height / 2 - view.getHeight());
ObjectAnimator rotation = ObjectAnimator.ofFloat(view, “rotation”, 0, 360);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, “scaleX”, 2.0f, 1.0f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, “scaleY”, 2.0f, 1.0f);
ObjectAnimator alpha = ObjectAnimator.ofFloat(view, “alpha”, 0f, 1.0f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(translationY, rotation, scaleX, scaleY, alpha);
animatorSet.setDuration(3000);
animatorSet.start();
return animatorSet;
}
}
SplashActivity
public class SplashActivity extends AppCompatActivity {
private ImageView img_animation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
initView();
}
private void initView() {
img_animation = findViewById(R.id.img_animation);
AnimatorUtil.setAnimatorSet(this,img_animation);
}
}
ICarView
public interface ICarView {
void showDataToCar(Context context, GoodCar goodCar);
GoodCar carData();
void goodSum(double sum);
}
CarActivity
public class CarActivity extends AppCompatActivity implements ICarView {
private TextView car_sum;
private CheckBox car_select_all;
private ExpandableListView car_elv;
private MyCarBaseAdapter myCarBaseAdapter;
private GoodCar goodCar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car);
initView();
PresenterImpl presenter = new PresenterImpl();
presenter.showDataToCar(CarActivity.this, new ModelImpl(), this);
}
private void initView() {
car_elv = findViewById(R.id.car_elv);
car_select_all = findViewById(R.id.car_select_all);
car_sum = findViewById(R.id.car_sum);
car_select_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<GoodCar.DataBean> data = goodCar.getData();
for (int i = 0; i < data.size(); i++) {
data.get(i).setGroup_flag(!data.get(i).getGroup_flag());
List<GoodCar.DataBean.ListBean> list = data.get(i).getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setChild_flag(!list.get(j).getChild_flag());
}
}
myCarBaseAdapter.notifyDataSetChanged();
//计算
PresenterImpl presenter = new PresenterImpl();
presenter.showGoodSum(new ModelImpl(),goodCar,CarActivity.this);
}
});
}
private static final String TAG = "CarActivity";
@Override
public void showDataToCar(Context context, GoodCar goodCar) {
this.goodCar = goodCar;
myCarBaseAdapter = new MyCarBaseAdapter(context, goodCar, this);
car_elv.setAdapter(myCarBaseAdapter);
int childCount = myCarBaseAdapter.getGroupCount();
for (int i = 0; i < childCount; i++) {
car_elv.expandGroup(i);
}
}
@Override
public GoodCar carData() {
return goodCar;
}
@Override
public void goodSum(double sum) {
car_sum.setText(sum + "");
}
}
IMainView
public interface IMainView {
void showDetailData(GoodDetail goodDetail);
void showAddSuccess();
void showAddError();
double getPid();
void addCarToView();
}
MainActivity
public class MainActivity extends AppCompatActivity implements IMainView, View.OnClickListener {
private SimpleDraweeView good_img;
private TextView good_title;
private TextView good_price;
private TextView good_bargainPrice;
private TextView good_add_car;
private TextView good_car;
private GoodDetail goodDetail;
private PresenterImpl presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fresco.initialize(this);
setContentView(R.layout.activity_main);
initView();
//调用Presenter进行数据加载
presenter = new PresenterImpl();
presenter.showDataToDetail(new ModelImpl(), this);
}
private void initView() {
good_img = findViewById(R.id.good_img);
good_title = findViewById(R.id.good_title);
good_price = findViewById(R.id.good_price);
good_bargainPrice = findViewById(R.id.good_bargainPrice);
good_add_car = findViewById(R.id.good_add_car);
good_car = findViewById(R.id.good_car);
good_add_car.setOnClickListener(this);
good_car.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.good_add_car://添加到购物车
presenter.showAddGoodToCar(new ModelImpl(), this);
break;
case R.id.good_car://查看购物车
CarActivity carActivity = new CarActivity();
startActivity(new Intent(MainActivity.this, CarActivity.class));
break;
}
}
private static final String TAG = "MainActivity";
@Override
public void showDetailData(GoodDetail goodDetail) {
this.goodDetail = goodDetail;
GoodDetail.DataBean dataBean = goodDetail.getData();
//设置数据
String images = dataBean.getImages();
String img = images.split(".jpg")[0] + ".jpg";
Uri img1 = Uri.parse(img);
//加载中显示的图片
DraweeController failureImageDraweeController = Fresco.newDraweeControllerBuilder()
.setUri(img1)
.setTapToRetryEnabled(false) //同时设置不可重试.
.setOldController(good_img.getController())
.build();
good_img.setController(failureImageDraweeController);
good_title.setText(dataBean.getTitle());
good_price.setText("原价:" + dataBean.getPrice() + "");
good_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
good_bargainPrice.setText("优惠价:" + dataBean.getBargainPrice() + "");
}
@Override
public void showAddSuccess() {
Toast.makeText(this, "添加成功!", Toast.LENGTH_SHORT).show();
}
@Override
public void showAddError() {
Toast.makeText(this, "添加失败!", Toast.LENGTH_SHORT).show();
}
@Override
public double getPid() {
Log.d(TAG, "getPid: ----" + goodDetail.getData().getPid());
return goodDetail.getData().getPid();
}
@Override
public void addCarToView() {
}
}
MyCarBaseAdapter
public class MyCarBaseAdapter extends BaseExpandableListAdapter {
Context context;
GoodCar goodCar;
ICarView iCarView;
private MyGroupViewHolder myGroupViewHolder;
private MyChildViewHolder myChildViewHolder;
private static final String TAG = “MyCarBaseAdapter”;
public MyCarBaseAdapter(Context context, GoodCar goodCar, ICarView iCarView) {
this.context = context;
this.goodCar = goodCar;
this.iCarView = iCarView;
Log.d(TAG, "MyCarBaseAdapter: ---++++++++++++++-" + goodCar.getData().get(0).getSellerName());
}
@Override
public int getGroupCount() {
return goodCar.getData().size();
}
@Override
public int getChildrenCount(int groupPosition) {
return goodCar.getData().get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return goodCar.getData().get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return goodCar.getData().get(groupPosition).getList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(context, R.layout.car_group, null);
myGroupViewHolder = new MyGroupViewHolder();
myGroupViewHolder.checkBox = convertView.findViewById(R.id.group_cb);
myGroupViewHolder.textView = convertView.findViewById(R.id.group_text);
convertView.setTag(myGroupViewHolder);
} else {
myGroupViewHolder = (MyGroupViewHolder) convertView.getTag();
}
myGroupViewHolder.checkBox.setChecked(goodCar.getData().get(groupPosition).getGroup_flag());
myGroupViewHolder.textView.setText(goodCar.getData().get(groupPosition).getSellerName());
myGroupViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked = goodCar.getData().get(groupPosition).getGroup_flag();
isChecked = !isChecked;
goodCar.getData().get(groupPosition).setGroup_flag(isChecked);
//遍历子元素,全部置为true
List<GoodCar.DataBean.ListBean> list = goodCar.getData().get(groupPosition).getList();
for (int i = 0; i < list.size(); i++) {
list.get(i).setChild_flag(isChecked);
}
//更新适配器
notifyDataSetChanged();
//计算
PresenterImpl presenter = new PresenterImpl();
presenter.showGoodSum(new ModelImpl(),goodCar,iCarView);
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
myChildViewHolder = new MyChildViewHolder();
convertView = View.inflate(context, R.layout.car_child, null);
myChildViewHolder.checkBox = convertView.findViewById(R.id.child_cb);
myChildViewHolder.textView1 = convertView.findViewById(R.id.child_name);
myChildViewHolder.textView2 = convertView.findViewById(R.id.child_bargainPrice);
myChildViewHolder.imageView = convertView.findViewById(R.id.child_img);
convertView.setTag(myChildViewHolder);
} else {
myChildViewHolder = (MyChildViewHolder) convertView.getTag();
}
final GoodCar.DataBean.ListBean listBean = goodCar.getData().get(groupPosition).getList().get(childPosition);
myChildViewHolder.checkBox.setChecked(listBean.getChild_flag());
myChildViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked = goodCar.getData().get(groupPosition).getList().get(childPosition).getChild_flag();
isChecked = !isChecked;
goodCar.getData().get(groupPosition).getList().get(childPosition).setChild_flag(isChecked);
List<GoodCar.DataBean.ListBean> list1 = goodCar.getData().get(groupPosition).getList();
boolean flag = true;
for (int i = 0; i < list1.size(); i++) {
if (!list1.get(i).getChild_flag()) {
flag = false;
}
}
goodCar.getData().get(groupPosition).setGroup_flag(flag);
notifyDataSetChanged();
//计算
PresenterImpl presenter = new PresenterImpl();
presenter.showGoodSum(new ModelImpl(),goodCar,iCarView);
}
});
myChildViewHolder.textView1.setText(listBean.getTitle());
myChildViewHolder.textView2.setText(listBean.getBargainPrice() + "");
String images = listBean.getImages();
String img = images.split(".jpg")[0] + ".jpg";
Glide.with(context).load(img).error(R.drawable.chiji).into(myChildViewHolder.imageView);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private class MyGroupViewHolder {
CheckBox checkBox;
TextView textView;
}
private class MyChildViewHolder {
CheckBox checkBox;
ImageView imageView;
TextView textView1;
TextView textView2;
}
}