购物车页面
public class WoDeFragment extends Fragment implements IView {
private View v;
private TextView price;
private TextView jiesuan;
private CheckBox quan;
private ExpandableListView expand;
private ShouYeFoodAdapter shouYeFoodAdapter;
private String mUrl = “http://www.wanandroid.com/tools/mockapi/6523/restaurant-list”;
private List<MyFoodData.DataBean> lists = new ArrayList<>();
private PresenterImpls presenterImpls;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_wo_de, container, false);
initView();
presenterImpls = new PresenterImpls(this);
presenterImpls.startRequest(mUrl);
shouYeFoodAdapter = new ShouYeFoodAdapter(lists,getActivity());
expand.setAdapter(shouYeFoodAdapter);
shouYeFoodAdapter.setCallBack(new ShouYeFoodAdapter.AdapterCallBack() {
@Override
public void shuaxin() {
setBottomFlushLayout();
}
});
quan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean allGoods = shouYeFoodAdapter.isAllGoods();
shouYeFoodAdapter.setAllGoods(!allGoods);
shouYeFoodAdapter.notifyDataSetChanged();
setBottomFlushLayout();
}
});
return v;
}
private void setBottomFlushLayout(){
boolean goods = shouYeFoodAdapter.isAllGoods();
quan.setChecked(goods);
Float allGoodsPrice = shouYeFoodAdapter.getAllGoodsPrice();
int allGoodsNumber = shouYeFoodAdapter.getAllGoodsNumber();
price.setText("价格是"+allGoodsPrice);
jiesuan.setText("去结算"+allGoodsNumber);
}
private void initView() {
quan = v.findViewById(R.id.quan);
price = v.findViewById(R.id.price);
jiesuan = v.findViewById(R.id.jisuan);
expand = v.findViewById(R.id.expandable);
expand.setGroupIndicator(null);
}
@Override
public void setSuccess(Object data) {
MyFoodData foodData = (MyFoodData) data;
lists.addAll(foodData.getData());
shouYeFoodAdapter.notifyDataSetChanged();
}
@Override
public void setError(Object errors) {
}
}
购物车xml页面
<RelativeLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rel">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/quan"/>
<TextView
android:text="价格"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/price" />
<TextView
android:id="@+id/jisuan"
android:text="去结算"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<ExpandableListView
android:layout_above="@id/rel"
android:id="@+id/expandable"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView
二级列表适配器
public class ShouYeFoodAdapter extends BaseExpandableListAdapter {
private List<MyFoodData.DataBean> mList;
private Context context;
public ShouYeFoodAdapter(List<MyFoodData.DataBean> mList, Context context) {
this.mList = mList;
this.context = context;
}
@Override
public int getGroupCount() {
return mList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mList.get(groupPosition).getSpus().size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder groupHolder = null;
if(convertView == null){
convertView = View.inflate(context,R.layout.wode_group_item,null);
groupHolder = new GroupHolder();
groupHolder.cb = convertView.findViewById(R.id.cb1);
groupHolder.titles = convertView.findViewById(R.id.titls);
convertView.setTag(groupHolder);
}else {
groupHolder = (GroupHolder)convertView.getTag();
}
MyFoodData.DataBean dataBean = mList.get(groupPosition);
groupHolder.titles.setText(dataBean.getName()+"");
boolean childAllCheck = isChildAllCheck(groupPosition);
groupHolder.cb.setChecked(childAllCheck);//child为false时,group不被选中,child为true时,group被选中;
groupHolder.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*if(adapterCallBack != null){
adapterCallBack.setGroupChecked(groupPosition);
}*/
;
childAllCheck(groupPosition,!isChildAllCheck(groupPosition));
notifyDataSetChanged();
if(adapterCallBack!= null){
adapterCallBack.shuaxin();//刷新底部视图
}
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder childHolder = null;
if(convertView == null){
convertView = View.inflate(context,R.layout.wode_food_item,null);
childHolder = new ChildHolder();
childHolder.cb = convertView.findViewById(R.id.cb);
childHolder.titles = convertView.findViewById(R.id.tits);
childHolder.imgs = convertView.findViewById(R.id.imgs);
childHolder.price = convertView.findViewById(R.id.prices);
childHolder.jiaJian = convertView.findViewById(R.id.jia_jian);
convertView.setTag(childHolder);
}else {
childHolder = (ChildHolder)convertView.getTag();
}
MyFoodData.DataBean.SpusBean spusBean = mList.get(groupPosition).getSpus().get(childPosition);
Glide.with(context).load(spusBean.getPic_url()).into(childHolder.imgs);
childHolder.titles.setText(spusBean.getName()+"");
childHolder.price.setText(spusBean.getSkus().get(0).getPrice()+"");
childHolder.jiaJian.setNums(spusBean.getPraise_num());
childHolder.cb.setChecked(spusBean.isChildChecked());
childHolder.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*if(adapterCallBack != null){
adapterCallBack.setChildChecked(groupPosition,childPosition);
}*/
boolean b= isChildChecked(groupPosition,childPosition);
setChildChecked(groupPosition,childPosition,!b);
notifyDataSetChanged();
if(adapterCallBack!= null){
adapterCallBack.shuaxin();//刷新底部视图
}
}
});
childHolder.jiaJian.setChange(new JiaJianView.OnCountChange() {
@Override
public void setCount(int con) {
setGoodsNums(groupPosition,childPosition,con);
notifyDataSetChanged();
if(adapterCallBack!= null){
adapterCallBack.shuaxin();//刷新底部视图
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class GroupHolder{
CheckBox cb;
TextView titles;
}
class ChildHolder{
CheckBox cb;
ImageView imgs;
TextView price;
TextView titles;
JiaJianView jiaJian;
}
/***
* 点击group的CheckBox让子控件全部选中
* 1.得到child的数据,for循环
* 2.判断子控件child是否被选中
*/
public void childAllCheck(int groupPosition ,boolean isCheck){
MyFoodData.DataBean dataBean = mList.get(groupPosition);
List<MyFoodData.DataBean.SpusBean> spus = dataBean.getSpus();
for (int i=0;i<spus.size();i++){
MyFoodData.DataBean.SpusBean spusBean = spus.get(i);
spusBean.setChildChecked(isCheck);
}
}
//判断child是否都被选中
public boolean isChildAllCheck(int groupPosition){
boolean boo = true;
MyFoodData.DataBean dataBean = mList.get(groupPosition);
List<MyFoodData.DataBean.SpusBean> spus = dataBean.getSpus();
for (int i=0;i<spus.size();i++){
MyFoodData.DataBean.SpusBean bean = spus.get(i);
if(!bean.isChildChecked()){//如果child有一个没被选中,则返回false
return false;
}
}
return boo;
}
//点击child给他赋值
public void setChildChecked(int groupPosition,int childPosition,boolean isChecked){
MyFoodData.DataBean.SpusBean spusBean = mList.get(groupPosition).getSpus().get(childPosition);
spusBean.setChildChecked(isChecked);
}
//查看当前商品child是否被选中
public boolean isChildChecked(int groupPosition,int childPosition){
MyFoodData.DataBean.SpusBean spusBean = mList.get(groupPosition).getSpus().get(childPosition);
if(spusBean.isChildChecked()){
return true;
}
return false;
}
//给商品的数量进行赋值
public void setGoodsNums(int groupPosition,int childPosition,int num){
MyFoodData.DataBean.SpusBean spusBean = mList.get(groupPosition).getSpus().get(childPosition);
spusBean.setPraise_num(num);
}
//给全选设置数据
public void setAllGoods(boolean isAllCheck){
for (int i = 0; i <mList.size(); i++) {
MyFoodData.DataBean dataBean = mList.get(i);
for (int j = 0; j <dataBean.getSpus().size() ; j++) {
MyFoodData.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
spusBean.setChildChecked(isAllCheck);
}
}
}
//底部视图的全选
public boolean isAllGoods(){
boolean boo = true;
for (int i = 0; i <mList.size(); i++) {
MyFoodData.DataBean dataBean = mList.get(i);
for (int j = 0; j <dataBean.getSpus().size() ; j++) {
MyFoodData.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
if(!spusBean.isChildChecked()){
boo = false;
}
}
}
return boo;
}
//计算所有商品的数量
public int getAllGoodsNumber(){
int allNumber = 0;
for (int i = 0; i <mList.size(); i++) {
MyFoodData.DataBean dataBean = mList.get(i);
for (int j = 0; j <dataBean.getSpus().size() ; j++) {
MyFoodData.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
if(spusBean.isChildChecked()){
allNumber += spusBean.getPraise_num();
}
}
}
return allNumber;
}
//计算所有商品的价格
public Float getAllGoodsPrice(){
float allPrice = 0;
for (int i = 0; i <mList.size(); i++) {
MyFoodData.DataBean dataBean = mList.get(i);
for (int j = 0; j <dataBean.getSpus().size() ; j++) {
MyFoodData.DataBean.SpusBean spusBean = dataBean.getSpus().get(j);
if(spusBean.isChildChecked()){
allPrice += spusBean.getPraise_num()*Float.parseFloat(spusBean.getSkus().get(0).getPrice());
}
}
}
return allPrice;
}
// 点击Group和Child的CheckBox在主页都需要需要刷新
public interface AdapterCallBack{
//group的刷新
void shuaxin();
}
private AdapterCallBack adapterCallBack;
public void setCallBack(AdapterCallBack adapterCallBack){
this.adapterCallBack = adapterCallBack;
}
}
加减自定义view
public class JiaJianView extends LinearLayout implements View.OnClickListener {
private TextView add;
private TextView remove;
private TextView number;
private int mCount;
public JiaJianView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.jiajian_item, this);
initView();
}
public void setNums(int num) {
this.mCount = num;
number.setText(num + "");
}
private void initView() {
remove = findViewById(R.id.jian);
remove.setOnClickListener(this);
add = findViewById(R.id.jia);
add.setOnClickListener(this);
number = findViewById(R.id.shu);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.jian:
if (mCount > 0) {
mCount--;
number.setText(mCount + "");
if (onCountChange != null) {
onCountChange.setCount(mCount);
}
}
break;
case R.id.jia:
mCount++;
number.setText(mCount + "");
if (onCountChange != null) {
onCountChange.setCount(mCount);
}
break;
}
}
public interface OnCountChange {
void setCount(int con);
}
private OnCountChange onCountChange;
public void setChange(OnCountChange onCountChange) {
this.onCountChange = onCountChange;
}
}