android实现折叠购物车计算了子类总价,和总计,子类多图片点击图片预览放大缩小等功 能是使用第三方库实现,页面样式可以自己去调整,地址:compile ‘com.ycjiang:ImagePreview:2.2.5’
增加了全选,父级和子级联动选择,要想显示子级上面的父级价格可以扩展
//想要显示父View的价格就设置expandableAdapter.setShowGroudView(true);
//不想显示父View的价格就删除expandableAdapter.setShowGroudView(true);这句话就行
要源码的朋友可以发邮箱
public class ExpandableActivity extends FragmentActivity implements ExpandableAdapter.OnCallBack, IZoomMediaLoader {
private ExpandableListView expandableListView;
private List list;
private List> lists;
private ExpandableAdapter expandableAdapter;
private TextView money;
private List listImage = new ArrayList<>();
private CheckBox all;
private Integer prices;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ex);
ZoomMediaLoader.getInstance().init(this);
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
money = (TextView) findViewById(R.id.money);
all = (CheckBox) findViewById(R.id.all);
list = new ArrayList<>();
lists = new ArrayList<>();
for (int s = 0; s < 3; s++) {
prices = 0;//初始化父类价格
ExpandablesBean expandablesBean = new ExpandablesBean();
expandablesBean.setName(s + "父下标");
expandablesBean.setStates(false);
list.add(expandablesBean);
List lists = new ArrayList<>();
for (int i = 0; i < 4; i++) {
ExpandablesBean.OrderList orderList = new ExpandablesBean.OrderList();
orderList.setName(1);
orderList.setPrice(10);
orderList.setStates(false);
lists.add(orderList);
for (int e = 0; e < 3; e++) {
if (e == 0) {
orderList.getUserViewInfos().add(new ExpandablesBean.OrderList.UserViewInfo("http://b-ssl.duitang.com/uploads/blog/201312/04/20131204184148_hhXUT.jpeg"));
} else if (e == 1) {
orderList.getUserViewInfos().add(new ExpandablesBean.OrderList.UserViewInfo("http://pic1.win4000.com/wallpaper/c/53cdd1f7c1f21.jpg"));
} else if (e == 2) {
orderList.getUserViewInfos().add(new ExpandablesBean.OrderList.UserViewInfo("http://b-ssl.duitang.com/uploads/blog/201312/04/20131204184148_hhXUT.jpeg"));
}
}
}
list.get(s).setList(lists);
}
expandableAdapter = new ExpandableAdapter(list, this, this);
//想要显示父View的价格就设置expandableAdapter.setShowGroudView(true);
//不想显示父View的价格就删除expandableAdapter.setShowGroudView(true);这句话就行
expandableAdapter.setShowGroudView(true);
expandableListView.setAdapter(expandableAdapter);
all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked = all.isChecked();
if (isChecked) {
setStates(isChecked);
} else {
setStates(isChecked);
}
}
});
}
/**
* 全选刷新当前状态
*
* @param isChecked
*/
private void setStates(boolean isChecked) {
for (int s = 0; s < list.size(); s++) {
list.get(s).setStates(isChecked);
prices = 0;
for (int e = 0; e < list.get(s).getList().size(); e++) {
list.get(s).getList().get(e).setStates(isChecked);
if (isChecked) {
prices += list.get(s).getList().get(e).getPrice();
list.get(s).setMoneys(prices);
} else {
list.get(s).setMoneys(0);
}
}
}
if (isChecked) {
setParentNumber();
} else {
money.setText("合计:" + 0);
moneysNumber = 0;
expandableAdapter.setList(list);
expandableAdapter.notifyDataSetChanged();
}
}
private Integer moneys = 0, moneysNumber = 0;
/**
* 点击减
*
* @param groupPosition
* @param childPosition
* @param view
*/
@Override
public void setCallBack(int groupPosition, int childPosition, View view, EditText editText, TextView textView) {
orderList = list.get(groupPosition).getList().get(childPosition);
if (orderList.getName() == 1) {//
Toast.makeText(this, "最少购买一件商品", Toast.LENGTH_SHORT).show();
} else {
Integer number = orderList.getName();
Integer price = orderList.getPrice();
price -= 10;//计算子类下的价格
number -= 1;//计算子类下的数量
setChild(number, price, editText, textView);//计算所有子类下的价格
list.get(groupPosition).getList().get(childPosition).setPrice(price);
setParent(groupPosition, list.get(groupPosition).getStates());
}
}
/**
* 点击加
*
* @param groupPosition
* @param childPosition
* @param view
*/
private ExpandablesBean.OrderList orderList;
@Override
public void setCallBacks(int groupPosition, int childPosition, View view, EditText editText, TextView textView) {
orderList = list.get(groupPosition).getList().get(childPosition);
Integer number = orderList.getName();
Integer price = orderList.getPrice();
number += 1;
price += 10;
setChild(number, price, editText, textView);//计算所有子类下的价格
setParent(groupPosition, list.get(groupPosition).getStates());
}
/**
* 点击加减按钮时候变化价格
*
* @param groupPosition
* @param childPosition
* @param isChecked
*/
private int whoState;
public void setCallBackChecked(int groupPosition, int childPosition, boolean isChecked) {
list.get(groupPosition).getList().get(childPosition).setStates(isChecked);
whoState = 2;
setParent(groupPosition, list.get(groupPosition).getStates());
}
/**
* 计算每个子类的价格
*/
private void setChild(Integer number, Integer price, EditText editText, TextView textView) {
orderList.setName(number);
orderList.setPrice(price);
editText.setText(orderList.getName() + "");
textView.setText(orderList.getPrice() + "");
}
/**
* 计算每个父类下所有子类的价格
*/
private List listState = new ArrayList<>();
public void setParent(int groupPosition, boolean isCheckedState) {
for (int s = 0; s < list.get(groupPosition).getList().size(); s++) {
if (list.get(groupPosition).getList().get(s).getPrice() != null) {
if (list.get(groupPosition).getList().get(s).getStates()) {
listState.add(s);
moneys += list.get(groupPosition).getList().get(s).getPrice();
}
}
}
if (whoState == 1) {//父View点击的
for (int s = 0; s < list.get(groupPosition).getList().size(); s++) {
list.get(groupPosition).getList().get(s).setStates(isCheckedState);
}
list.get(groupPosition).setStates(isCheckedState);
list.get(groupPosition).setMoneys(moneys);
} else {//子View点击的
if (listState.size() == list.get(groupPosition).getList().size()) {
list.get(groupPosition).setStates(true);
list.get(groupPosition).setMoneys(moneys);
} else {
list.get(groupPosition).setStates(false);
list.get(groupPosition).setMoneys(moneys);
}
}
moneys = 0;
listState.clear();
setParentNumber();
}
/**
* 计算每个父类下所有子类的价格
*/
public void setParentIsChecked(int groupPosition, boolean isChecked) {
whoState = 1;
System.out.println(isChecked + "王");
List orderLists = null;
for (int s = 0; s < list.get(groupPosition).getList().size(); s++) {
orderLists = list.get(groupPosition).getList();
orderLists.get(s).setStates(isChecked);
}
list.get(groupPosition).setList(orderLists);
list.get(groupPosition).setStates(isChecked);
}
/**
* 计算最终价格
*/
private int state;
private List listes = new ArrayList<>();
public void setParentNumber() {
state = 1;
for (int i = 0; i < list.size(); i++) {//计算所有父类下的价格
if (list.get(i).getMoneys() != null) {
if (list.get(i).getStates()) {
listes.add(i);
moneysNumber += list.get(i).getMoneys();
state = 2;
}
}
}
money.setText("合计:" + moneysNumber);
moneysNumber = 0;
expandableAdapter.setList(list);
if (state == 2) {
if (list.size() == listes.size()) {
all.setChecked(true);
} else {
all.setChecked(false);
}
expandableAdapter.notifyDataSetChanged();
} else {
all.setChecked(false);
expandableAdapter.notifyDataSetChanged();
}
listes.clear();
}
@Override
public void displayImage(@NonNull Fragment context, @NonNull String path, ImageView imageView, @NonNull final MySimpleTarget simpleTarget) {
Glide.with(context).load(path)
.asBitmap()
.fitCenter()
.centerCrop()
.listener(new RequestListener() {
@Override
public boolean onException(Exception e, String model, Target target, boolean isFirstResource) {
simpleTarget.onLoadFailed(null);
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) {
simpleTarget.onResourceReady();
return false;
}
})
.into(imageView);
}
@Override
public void displayGifImage(@NonNull Fragment context, @NonNull String path, ImageView imageView, @NonNull final MySimpleTarget simpleTarget) {
Glide.with(context).load(path)
.asGif()
//可以解决gif比较几种时 ,加载过慢 //DiskCacheStrategy.NONE
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.dontAnimate() //去掉显示动画
.listener(new RequestListener() {
@Override
public boolean onException(Exception e, String model, Target target, boolean isFirstResource) {
simpleTarget.onResourceReady();
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) {
simpleTarget.onLoadFailed(null);
return false;
}
})
.into(imageView);
}
@Override
public void onStop(@NonNull Fragment context) {
Glide.with(context).onStop();
}
@Override
public void clearMemory(@NonNull Context c) {
Glide.get(c).clearMemory();
}
}