MainActivity(动画):
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView =(ImageView) findViewById(R.id.image_view);
//平移
ObjectAnimator y = ObjectAnimator.ofFloat(imageView, "Y", 0, 500);
//透明度
ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1);
//旋转
ObjectAnimator rotationX = ObjectAnimator.ofFloat(imageView, "rotationX", 0, 360);
ObjectAnimator rotationY = ObjectAnimator.ofFloat(imageView, "rotationY", 0, 360);
//缩放
ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 2f, 1);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 2f, 1);
//组合
AnimatorSet animatorSet = new AnimatorSet();
//放到一起
animatorSet.play(y).with(alpha).with(rotationX).with(rotationY).with(scaleX).with(scaleY);
animatorSet.setDuration(3000);
animatorSet.start();
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
Intent intent=new Intent(MainActivity.this,GoodsXiangqingActivity.class);
startActivity(intent);
finish();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
MainActivity布局中:
<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" tools:context="com.example.yuekao_dome01.view.MainActivity"
android:orientation="vertical"
android:background="#f4ff"
>
<ImageView
android:layout_gravity="center"
android:id="@+id/image_view"
android:src="@mipmap/ic_launcher_round"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
GoodsXiangqingActivity(商品详情):
public class GoodsXiangqingActivity extends AppCompatActivity implements IGoodsView,IAddGoodsView {
private ImageView xiangqing_image;
private TextView xiangqing_title;
private TextView xiangqing_price;
private GoodsPresenter goodsPresenter;
int pid=69;
int uid=10863;
private AddGoodsPresenter addGoodsPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goods_xiangqing);
xiangqing_image = (ImageView) findViewById(R.id.xiangqing_image);
xiangqing_title = (TextView) findViewById(R.id.xiangqing_title);
xiangqing_price = (TextView) findViewById(R.id.xiangqing_price);
goodsPresenter = new GoodsPresenter(this);
goodsPresenter.getGoods("https://www.zhaoapi.cn/product/getProductDetail",pid);
}
public void getfanhui(View view) {
finish();
}
@Override
public void postGoods(final GoodsBean goodsBean) {
runOnUiThread(new Runnable() {
@Override
public void run() {
xiangqing_title.setText(goodsBean.getData().getTitle()+"");
xiangqing_price.setText(goodsBean.getData().getBargainPrice()+"");
String[] split = goodsBean.getData().getImages().split("\\|");
Glide.with(GoodsXiangqingActivity.this).load(split[0]).into(xiangqing_image);
}
});
}
//点击进入购物车
public void Cart(View view) {
Intent intent=new Intent(this,GouwucheActivity.class);
intent.putExtra("uid",uid);
startActivity(intent);
}
//添加购物车
public void addCart(View view) {
addGoodsPresenter = new AddGoodsPresenter(this);
addGoodsPresenter.getAddGoods("https://www.zhaoapi.cn/product/addCart",uid,pid);
}
@Override
public void postAddGoods(final AddGoodsBean addGoodsBean) {
runOnUiThread(new Runnable() {
@Override
public void run() {
int code = Integer.parseInt(addGoodsBean.getCode());
if(code==0){
Toast.makeText(GoodsXiangqingActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
}else if(code==1){
Toast.makeText(GoodsXiangqingActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
}
}
});
}
}
GoodsXiangqingActivity布局:
<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"
tools:context="com.example.yuekao_dome01.view.GoodsXiangqingActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:paddingTop="10dp"
android:orientation="horizontal">
<Button
android:onClick="getfanhui"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="9"
android:background="@drawable/leftjiantou" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_weight="1"
android:text="商品"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="9"
android:orientation="vertical">
<ImageView
android:id="@+id/xiangqing_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/xiangqing_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/xiangqing_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:onClick="Cart"
android:text="购物车"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:onClick="addCart"
android:background="#f00"
android:text="加入购物车"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
GouwucheActivity(购物车):
public class GouwucheActivity extends AppCompatActivity implements IGouwucheView, View.OnClickListener {
private MyExpanableListView expanable_list_view;
private RelativeLayout relative_progress;
private CheckBox check_all;
private TextView text_total;
private TextView text_buy;
private GouwuchePresenter gouwuchePresenter;
private MyGouwucheAdapter myGouwucheAdapter;
private GouwucheCountPriceBean gouwucheCountPriceBean;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
gouwucheCountPriceBean = (GouwucheCountPriceBean) msg.obj;
text_total.setText("合计:¥" + gouwucheCountPriceBean.getPriceString());
text_buy.setText("去结算(" + gouwucheCountPriceBean.getCount() + ")");
}
}
};
private int uid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gouwuche);
expanable_list_view = (MyExpanableListView) findViewById(R.id.expanable_list_view);
relative_progress = (RelativeLayout) findViewById(R.id.relative_progress);
check_all = (CheckBox) findViewById(R.id.check_all);
text_total = (TextView) findViewById(R.id.text_total);
text_buy = (TextView) findViewById(R.id.text_buy);
text_buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(GouwucheActivity.this,DiangdanActivity.class);
startActivity(intent);
}
});
//接受uid
Intent intent = getIntent();
uid = intent.getIntExtra("uid", 10863);
gouwuchePresenter = new GouwuchePresenter(this);
//去掉默认的指示器
expanable_list_view.setGroupIndicator(null);
//全选设置点击事件
check_all.setOnClickListener(GouwucheActivity.this);
}
@Override
protected void onResume() {
super.onResume();
gouwuchePresenter.getGouwuche("https://www.zhaoapi.cn/product/getCarts", String.valueOf(uid));
}
@Override
public void postGouwuche(final GouwucheBean gouwucheBean) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//获取数据成功隐藏
relative_progress.setVisibility(View.GONE);
if (gouwucheBean != null) {
//第四步:需要更改获取的cartBean数据
//1..根据某一个组中的二级所有的子条目是否选中,确定当前一级列表是否选中
for (int i = 0; i < gouwucheBean.getData().size(); i++) {
if (isChildInGroupChecked(i, gouwucheBean)) {
//如果选中就为true
gouwucheBean.getData().get(i).setGroup_check(true);
}
}
//2.设置是否全选选中...根据所有的一级列表是否选中,确定全选是否选中
check_all.setChecked(isAllGroupChecked(gouwucheBean));
myGouwucheAdapter = new MyGouwucheAdapter(GouwucheActivity.this, gouwucheBean, handler, gouwuchePresenter, relative_progress,uid);
expanable_list_view.setAdapter(myGouwucheAdapter);
//第三步
// 展开所有的组
for (int i = 0; i < gouwucheBean.getData().size(); i++) {
expanable_list_view.expandGroup(i);
}
//3.计算总价和商品的数量
myGouwucheAdapter.sendPriceAndCount();
} else {
Toast.makeText(GouwucheActivity.this, "购物车为空,快去添加吧!!", Toast.LENGTH_SHORT).show();
}
}
});
}
//第四步 2.生成的:所有的组是否选中
private boolean isAllGroupChecked(GouwucheBean gouwucheBean) {
for (int i = 0; i < gouwucheBean.getData().size(); i++) {
//表示有没选中的组
if (!gouwucheBean.getData().get(i).isGroup_check()) {
return false;
}
}
return true;
}
//第四步 1.生成的:当前组中所有的子条目是否全部选中
private boolean isChildInGroupChecked(int i, GouwucheBean gouwucheBean) {
//当前组中所有子条目的数据
List<GouwucheBean.DataBean.ListBean> list = gouwucheBean.getData().get(i).getList();
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getSelected() == 0) {//有未选中的条目
return false;
}
}
return true;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.check_all:
//所有孩子的状态跟着全选的状态进行改变
if (myGouwucheAdapter != null) {
myGouwucheAdapter.setAllChildChecked(check_all.isChecked());
}
break;
}
}
}
GouwucheActivity布局:
<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="com.example.yuekao_dome01.view.GouwucheActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!--二级列表-->
<com.example.yuekao_dome01.util.MyExpanableListView
android:id="@+id/expanable_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.yuekao_dome01.util.MyExpanableListView>
<!--recyclerView展示为你推荐-->
<TextView
android:background="#00ff00"
android:text="为你推荐"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="400dp" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/relative_progress"
android:visibility="gone"
android:layout_above="@+id/linear_bottom"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/linear_bottom"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="50dp">
<CheckBox
android:button="@null"
android:background="@drawable/check_box_selector"
android:layout_marginLeft="10dp"
android:id="@+id/check_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_total"
android:text="合计:¥0.00"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_buy"
android:background="#ff0000"
android:textColor="#ffffff"
android:gravity="center"
android:text="去结算(0)"
android:layout_width="100dp"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
MyGouwucheAdapter(adapter操作):
public class MyGouwucheAdapter extends BaseExpandableListAdapter {
private int uid;
private RelativeLayout relative_progress;
private GouwuchePresenter gouwuchePresenter;
private Handler handler;
private Context context;
private GouwucheBean gouwucheBean;
private int childIndex;
private int allIndex;
public MyGouwucheAdapter(Context context, GouwucheBean gouwucheBean, Handler handler, GouwuchePresenter gouwuchePresenter, RelativeLayout relative_progress, int uid) {
this.context = context;
this.gouwucheBean = gouwucheBean;
this.handler=handler;
this.gouwuchePresenter=gouwuchePresenter;
this.relative_progress=relative_progress;
this.uid=uid;
}
@Override
public int getGroupCount() {
return gouwucheBean.getData().size();
}
@Override
public int getChildrenCount(int groupPosition) {
return gouwucheBean.getData().get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return gouwucheBean.getData().get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return gouwucheBean.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 true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final GroupHolder holder;
if(convertView==null){
convertView = View.inflate(context, R.layout.group_layout, null);
holder=new GroupHolder();
holder.checkBox =(CheckBox) convertView.findViewById(R.id.group_check);
holder.textView=(TextView) convertView.findViewById(R.id.group_text);
convertView.setTag(holder);
}else{
holder=(GroupHolder) convertView.getTag();
}
final GouwucheBean.DataBean dataBean = gouwucheBean.getData().get(groupPosition);
holder.textView.setText(dataBean.getSellerName());
holder.checkBox.setChecked(dataBean.isGroup_check());
//一级列表的点击事件
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
relative_progress.setVisibility(View.VISIBLE);
//点击一级列表的时候,子条目需要一个个的去执行更新,,,,全都执行完成之后再去请求查询购物车
//使用递归的形式...例如一组里面3个孩子,,,0,1,2
childIndex = 0;
//更新
updateChildCheckedInGroup(holder.checkBox.isChecked(),dataBean);
}
});
return convertView;
}
private void updateChildCheckedInGroup(final boolean checked, final GouwucheBean.DataBean dataBean) {
GouwucheBean.DataBean.ListBean listBean1 = dataBean.getList().get(childIndex);
Map<String, String> map=new HashMap<>();
map.put("uid", String.valueOf(uid));
map.put("pid", listBean1.getPid() + "");
map.put("selected", String.valueOf(checked ? 1:0));
map.put("num", listBean1.getNum() + "");
map.put("sellerid", listBean1.getSellerid() + "");
map.put("source","android");
OkHttp3Util.doPost(ApiUtil.gengxingouwucheurl, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
childIndex++;
if(childIndex<dataBean.getList().size()){
//继续更新
updateChildCheckedInGroup(checked,dataBean);
}else{
//请求查询购物车的操作,,,重新展示数据
gouwuchePresenter.getGouwuche(ApiUtil.chaxungouwucheurl,uid+"");
}
}
}
});
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
if(convertView==null){
convertView= View.inflate(context, R.layout.child_layout, null);
holder=new ChildHolder();
holder.checkBox =(CheckBox) convertView.findViewById(R.id.child_check);
holder.imageView=(ImageView) convertView.findViewById(R.id.child_image);
holder.text_title=(TextView) convertView.findViewById(R.id.child_title);
holder.text_price=(TextView) convertView.findViewById(R.id.child_price);
holder.text_num=(TextView) convertView.findViewById(R.id.text_num);
holder.text_add=(TextView) convertView.findViewById(R.id.text_add);
holder.text_jian=(TextView) convertView.findViewById(R.id.text_jian);
holder.text_delete=(TextView) convertView.findViewById(R.id.text_delete);
convertView.setTag(holder);
}else{
holder=(ChildHolder) convertView.getTag();
}
final GouwucheBean.DataBean.ListBean listBean = gouwucheBean.getData().get(groupPosition).getList().get(childPosition);
//赋值
holder.text_title.setText(listBean.getTitle());
holder.text_price.setText("¥"+listBean.getBargainPrice());
holder.text_num.setText(listBean.getNum()+"");
String[] strings = listBean.getImages().split("\\|");
holder.checkBox.setChecked(listBean.getSelected() == 0? false:true);//根据0,1进行设置是否选中
Glide.with(context).load(strings[0]).into(holder.imageView);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//progressBar要显示
relative_progress.setVisibility(View.VISIBLE);
Map<String, String> map=new HashMap<String, String>();
map.put("uid", String.valueOf(uid));
map.put("pid",listBean.getPid()+"");
map.put("selected", String.valueOf(listBean.getSelected()==0?1:0));
map.put("num",listBean.getNum()+"");
map.put("sellerid",listBean.getSellerid()+"");
map.put("source","android");
OkHttp3Util.doPost(ApiUtil.gengxingouwucheurl, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
gouwuchePresenter.getGouwuche(ApiUtil.chaxungouwucheurl,uid+"");
}
}
});
}
});
holder.text_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//progressBar要显示
relative_progress.setVisibility(View.VISIBLE);
Map<String, String> map=new HashMap<String, String>();
map.put("uid", String.valueOf(uid));
map.put("pid",listBean.getPid()+"");
map.put("selected", String.valueOf(listBean.getSelected()));
map.put("num", String.valueOf(listBean.getNum()+1));
map.put("sellerid",listBean.getSellerid()+"");
map.put("source","android");
OkHttp3Util.doPost(ApiUtil.gengxingouwucheurl, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
gouwuchePresenter.getGouwuche(ApiUtil.chaxungouwucheurl,uid+"");
}
}
});
}
});
//减
holder.text_jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num = listBean.getNum();
if(num==1){
return;
}
//progressBar要显示
relative_progress.setVisibility(View.VISIBLE);
//请求更新购物车的接口。。。更新成功之后,再次请求查询购物车的接口,进行数据的展示
Map<String, String> map=new HashMap<String, String>();
map.put("uid", String.valueOf(uid));
map.put("pid",listBean.getPid()+"");
map.put("selected", String.valueOf(listBean.getSelected()));
map.put("num", String.valueOf(num-1));
map.put("sellerid",listBean.getSellerid()+"");
map.put("source","android");
OkHttp3Util.doPost(ApiUtil.gengxingouwucheurl, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
gouwuchePresenter.getGouwuche(ApiUtil.chaxungouwucheurl,uid+"");
}
}
});
}
});
//删除
holder.text_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//显示progress
relative_progress.setVisibility(View.VISIBLE);
//uid=72&pid=1&source=android
Map<String, String> params = new HashMap<>();
params.put("uid", String.valueOf(uid));
params.put("pid", String.valueOf(listBean.getPid()));
params.put("source","android");
OkHttp3Util.doPost(ApiUtil.shanchugouwucheurl, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
//删除成功之后.....再次查询购物车
gouwuchePresenter.getGouwuche(ApiUtil.chaxungouwucheurl,uid+"");
}
}
});
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
/**
* 计算数量和价格,,,并发送到activity显示
*/
public void sendPriceAndCount() {
double price=0;
int count=0;
for(int i=0;i<gouwucheBean.getData().size();i++){
List<GouwucheBean.DataBean.ListBean> list = gouwucheBean.getData().get(i).getList();
for(int j=0;j<list.size();j++){
//拿到每一个子条目
GouwucheBean.DataBean.ListBean listBean = list.get(j);
//选中的时候计算价格跟数量
if(listBean.getSelected()==1){
price+=listBean.getBargainPrice()*listBean.getNum();
count+=listBean.getNum();
}
}
}
//给价格保留两位小数
DecimalFormat decimalFormat = new DecimalFormat("0.00");
String priceString = decimalFormat.format(price);
//封装一下
GouwucheCountPriceBean gouwucheCountPriceBean=new GouwucheCountPriceBean(priceString,count);
//发送给gouwuche_Frangment进行展示
Message msg=Message.obtain();
msg.what=0;
//obj只能传一个数据,因为我们有两条数据所以我们要封装一下
msg.obj=gouwucheCountPriceBean;
handler.sendMessage(msg);
}
//更新所有的子孩子的状态...跟随全选状态改变
public void setAllChildChecked(boolean checked) {
//显示进度条
relative_progress.setVisibility(View.VISIBLE);
//通过遍历,吧所有的孩子装到一个大的集合中
GouwucheBean.DataBean.ListBean listBean = gouwucheBean.getData().get(0).getList().get(0);
List<GouwucheBean.DataBean.ListBean> allList=new ArrayList<>();
for (int i = 0; i <gouwucheBean.getData().size() ; i++) {
for (int j = 0; j < gouwucheBean.getData().get(i).getList().size(); j++) {
allList.add(gouwucheBean.getData().get(i).getList().get(j));
}
}
//更新每一个子孩子的状态....递归
allIndex = 0;
updateAllChild(checked,allList);
}
private void updateAllChild(final boolean checked, final List<GouwucheBean.DataBean.ListBean> allList) {
GouwucheBean.DataBean.ListBean listBean = allList.get(allIndex);
//请求更新购物车的接口。。。更新成功之后,再次请求查询购物车的接口,进行数据的展示
Map<String, String> map = new HashMap<String, String>();
map.put("uid", String.valueOf(uid));
map.put("pid", listBean.getPid() + "");
map.put("selected", String.valueOf(checked? 1:0));
map.put("num", String.valueOf(listBean.getNum()));
map.put("sellerid", listBean.getSellerid() + "");
map.put("source","android");
OkHttp3Util.doPost(ApiUtil.gengxingouwucheurl, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
allIndex ++;
//判断
if(allIndex<allList.size()){
//继续更新
updateAllChild(checked,allList);
}else{
//查询购物车
gouwuchePresenter.getGouwuche(ApiUtil.chaxungouwucheurl,uid+"");
}
}
}
});
}
private class GroupHolder{
CheckBox checkBox;
TextView textView;
}
private class ChildHolder{
CheckBox checkBox;
ImageView imageView;
TextView text_title;
TextView text_price;
TextView text_num;
TextView text_add;
TextView text_jian;
TextView text_delete;
}
}
GouwuchePresenter(MVP P层):
public class GouwuchePresenter implements IGouwuchePresenter {
private IGouwucheView iGouwucheView;
private GouwucheModel gouwucheModel;
public GouwuchePresenter(IGouwucheView iGouwucheView) {
this.iGouwucheView=iGouwucheView;
}
public void getGouwuche(String url, String uid) {
gouwucheModel = new GouwucheModel(this);
gouwucheModel.getGouwuche(url,uid);
}
@Override
public void postGouwuche(GouwucheBean gouwucheBean) {
iGouwucheView.postGouwuche(gouwucheBean);
}
}
GouwucheModel(MVP M层):
public class GouwucheModel {
private IGouwuchePresenter iGouwuchePresenter;
public GouwucheModel(IGouwuchePresenter iGouwuchePresenter) {
this.iGouwuchePresenter=iGouwuchePresenter;
}
public void getGouwuche(String url, String uid) {
Map<String, String> map=new HashMap<>();
map.put("uid",uid);
map.put("source","android");
OkHttp3Util.doPost(url, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
String json = response.body().string();
Log.i(json,"zxc");
if("null".equals(json)){
iGouwuchePresenter.postGouwuche(null);
}else{
Gson gson=new Gson();
GouwucheBean gouwucheBean = gson.fromJson(json, GouwucheBean.class);
iGouwuchePresenter.postGouwuche(gouwucheBean);
}
}
}
});
}
}
MyExpanableListView:(在购物车布局中二级列表需要)
public class MyExpanableListView extends ExpandableListView {
public MyExpanableListView(Context context) {
super(context);
}
public MyExpanableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyExpanableListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//高度
int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, height);
}
}
Child_layout(子布局):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="wrap_content">
<CheckBox
android:button="@null"
android:background="@drawable/check_box_selector"
android:id="@+id/child_check"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/child_check"
android:id="@+id/child_image"
android:layout_width="100dp"
android:layout_height="100dp"
/>
<TextView
android:maxLines="2"
android:minLines="2"
android:id="@+id/child_title"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/child_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/child_price"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/child_image"
android:layout_alignBottom="@+id/child_image"
android:textColor="#ff44"
android:text="¥0.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/child_image"
android:layout_toLeftOf="@+id/text_delete"
android:orientation="horizontal">
<TextView
android:id="@+id/text_jian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bian_kuang"
android:padding="5dp"
android:text="-" />
<TextView
android:id="@+id/text_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bian_kuang"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:text="1" />
<TextView
android:id="@+id/text_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bian_kuang"
android:padding="5dp"
android:text="+" />
</LinearLayout>
<TextView
android:id="@+id/text_delete"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:background="#ff0000"
android:gravity="center"
android:text="删除"
android:textColor="#ffffff" />
</RelativeLayout>
group_layout主布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="horizontal">
<CheckBox
android:button="@null"
android:background="@drawable/check_box_selector"
android:id="@+id/group_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="@+id/group_text"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
child_layout边框
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<stroke android:color="#000000" android:width="0.1dp"/>
</shape>
图片点击选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/shopping_cart_checked"/>
<item android:state_checked="false" android:drawable="@drawable/shopping_cart_none_check"/>
<item android:drawable="@drawable/shopping_cart_none_check"/>
</selector>