安卓shopDemo

//导入依赖
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'org.greenrobot:eventbus:3.1.1'
//用MVP开发购物车
//适配器页面
package mvpframework.bwie.com.myshopapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import mvpframework.bwie.com.myshopapp.R;
import mvpframework.bwie.com.myshopapp.bean.ShopBean;
import mvpframework.bwie.com.myshopapp.eventbusevent.MessageEvent;
import mvpframework.bwie.com.myshopapp.eventbusevent.PriceAndCountEvent;

/**
 * Created by 杨群 on 2017/11/18.
 */

public class MyAdapter extends BaseExpandableListAdapter {
  private Context context;
    private List<ShopBean.DataBean> groupList;
    private List<List<ShopBean.DataBean.ListBean>> childList;
    private final LayoutInflater inflater;

    public MyAdapter(Context context, List<ShopBean.DataBean> groupList, List<List<ShopBean.DataBean.ListBean>> childList) {
        this.groupList = groupList;
        this.context = context;
        this.childList = childList;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).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, final View convertView, ViewGroup parent) {
        View view;
        final GroupViewHolder holder;
        if(convertView == null){
            holder=new GroupViewHolder();
            view=inflater.inflate(R.layout.item_parent_market,null);
            holder.cbGroup = view.findViewById(R.id.cb_parent);
            holder.tv_number = view.findViewById(R.id.tv_number);
            holder.tv_sign=view.findViewById(R.id.tv_sign);
            view.setTag(holder);
        }else{
            view=convertView;
            holder= (GroupViewHolder) view.getTag();
        }
        final ShopBean.DataBean dataBean = groupList.get(groupPosition);
        holder.cbGroup.setChecked(dataBean.isChecked());
        holder.tv_number.setText(dataBean.getSellerid());
        holder.tv_sign.setText(dataBean.getSellerName());
         holder.cbGroup.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 dataBean.setChecked(holder.cbGroup.isChecked());
                  changeChildCbState(groupPosition,holder.cbGroup.isChecked());
                 EventBus.getDefault().post(compute());
                 changeAllCbState(isAllGroupCbSelected());
                 notifyDataSetChanged();
             }
         });
        return view;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isExpanded, View convertView, ViewGroup parent) {
       View view;
        final ChildViewHolder cholder;
        if(convertView == null){
            cholder=new ChildViewHolder();
            view=inflater.inflate(R.layout.item_child_market,null);
            cholder.cbChild = view.findViewById(R.id.cb_child);
            cholder.tv_tel = view.findViewById(R.id.tv_tel);

            cholder.tv_time = view.findViewById(R.id.tv_time);
            cholder.tv_price = view.findViewById(R.id.tv_pri);
            cholder.tv_del = view.findViewById(R.id.tv_del);
            cholder.iv_add = view.findViewById(R.id.iv_add);
            cholder.iv_del = view.findViewById(R.id.iv_del);
            cholder.tv_num = view.findViewById(R.id.tv_num);
            cholder.iv=view.findViewById(R.id.iv);
            view.setTag(cholder);

        }else{
            view=convertView;
            cholder= (ChildViewHolder) view.getTag();
        }
        final ShopBean.DataBean.ListBean listBean = childList.get(groupPosition).get(childPosition);
        cholder.cbChild.setChecked(listBean.isChecked());
        cholder.tv_tel.setText(listBean.getTitle());
        cholder.tv_time.setText(listBean.getCreatetime());
        cholder.tv_price.setText(listBean.getPrice() + "");
        cholder.tv_num.setText(listBean.getNum() + "");
        String[] split=listBean.getImages().split("\\|");

         ImageLoader.getInstance().displayImage(split[0] ,cholder.iv);
        cholder.cbChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置该条目对象里的checked属性值
                listBean.setChecked(cholder.cbChild.isChecked());
              PriceAndCountEvent priceAndCountEvent=compute();
                EventBus.getDefault().post(priceAndCountEvent);
              if(cholder.cbChild.isChecked()){
                  //当前checkbox是选中状态
                  if(isAllChildCbSelected(groupPosition)){
                   changGroupCbState(groupPosition,true);
                      changeAllCbState(isAllGroupCbSelected());
                  }else{
                      changGroupCbState(groupPosition,false);
                      changeAllCbState(isAllGroupCbSelected());
                  }
                  notifyDataSetChanged();
              }
            }
        });
        //减
        cholder.iv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num=listBean.getNum();
                if(num==1){
                    return;
                }
                cholder.tv_num.setText(--num+"");
                listBean.setNum(num);
                if(cholder.cbChild.isChecked()){
                   PriceAndCountEvent priceAndCountEvent=compute();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });
        //加
        cholder.iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num=listBean.getNum();
                cholder.tv_num.setText(++num+"");
                listBean.setNum(num);
                if(cholder.cbChild.isChecked()){
                    PriceAndCountEvent priceAndCountEvent=compute();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });
        //删除
        cholder.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<ShopBean.DataBean.ListBean> listBeens = childList.get(groupPosition);
                ShopBean.DataBean.ListBean remove = listBeens.remove(childPosition);
               if(listBeens.size() ==0){
                   childList.remove(groupPosition);
                   groupList.remove(groupPosition);
               }
                EventBus.getDefault().post(compute());
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    class GroupViewHolder{
        CheckBox cbGroup;
        TextView tv_number;
        TextView tv_sign;
    }
    class ChildViewHolder {
        CheckBox cbChild;
        ImageView iv;
        TextView tv_tel;
        TextView tv_time;
        TextView tv_price;
        TextView tv_del;
        ImageView iv_del;
        ImageView iv_add;
        TextView tv_num;
    }
    /**
     * 计算列表中,选中的钱和数量
     */
    private PriceAndCountEvent compute(){
        int count=0;
        int price=0;
        for(int i=0;i<childList.size();i++){
            List<ShopBean.DataBean.ListBean> listBeen = childList.get(i);
            for(int j=0;j<listBeen.size();j++){
                ShopBean.DataBean.ListBean listBean = listBeen.get(j);
                if(listBean.isChecked()){
                    price+=listBean.getNum()*listBean.getPrice();
                    count += listBean.getNum();
                }
            }
        }
        PriceAndCountEvent priceAndCountEvent=new PriceAndCountEvent();
        priceAndCountEvent.setCount(count);
        priceAndCountEvent.setPrice(price);
        return priceAndCountEvent;
    }
    /**
     * 判断一级列表是否全部选中
     *
     * @return
     */
    private boolean isAllGroupCbSelected() {
        for (int i = 0; i < groupList.size(); i++) {
            ShopBean.DataBean dataBean = groupList.get(i);
            if (!dataBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断二级列表是否全部选中
     *
     * @param groupPosition
     * @return
     */
    private boolean isAllChildCbSelected(int groupPosition){
        List<ShopBean.DataBean.ListBean> been = childList.get(groupPosition);
        for(int i=0;i<been.size();i++){
            ShopBean.DataBean.ListBean ListBean = been.get(i);
          if(!ListBean.isChecked()){
              return false;
          }

        }
        return true;
    }
    /**
     * 改变一级列表checkbox状态
     *
     * @param groupPosition
     */
    private void changGroupCbState(int groupPosition,boolean flag){
        ShopBean.DataBean dataList = groupList.get(groupPosition);
        dataList.setChecked(flag);
    }
    //改变二级列表checkbox状态
    private void changeChildCbState(int groupPosition,boolean flag){
        List<ShopBean.DataBean.ListBean> datasBeens = childList.get(groupPosition);
       for(int i=0;i<datasBeens.size();i++){
           ShopBean.DataBean.ListBean datasBeen = datasBeens.get(i);
           datasBeen.setChecked(flag);
       }
    }
    /**
     * 改变全选的状态
     *
     * @param flag
     */
    private void changeAllCbState(boolean flag){
        MessageEvent messageEvent=new MessageEvent();
        messageEvent.setChecked(flag);
        EventBus.getDefault().post(messageEvent);
    }
    /**
     * 设置全选、反选
     *
     * @param flag
     */
    public void changeAllListCbState(boolean flag) {
        for (int i = 0; i < groupList.size(); i++) {
            changGroupCbState(i, flag);
            changeChildCbState(i, flag);
        }
        EventBus.getDefault().post(compute());
        notifyDataSetChanged();
    }
}

//网络工具utils的HttpUtils
package mvpframework.bwie.com.myshopapp.net;

import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class HttpUtils {
    private static volatile HttpUtils httpUtils;
    private final  OkHttpClient client;
    private HttpUtils(){
       client = new OkHttpClient.Builder().build();
    }
    public static HttpUtils getHttpUtils(){
        if(httpUtils == null){
            synchronized (HttpUtils.class){
                if(httpUtils == null){
                    httpUtils=new HttpUtils();
                }
            }
        }
        return httpUtils;
    }
    /**
     * GET请求
     *
     * @param url
     * @param callback
     */
    public void doGet(String url, Callback callback){
        Request request = new Request.Builder().url(url).build();
       client.newCall(request).enqueue(callback);
    }
}
//OnNetListener
package mvpframework.bwie.com.myshopapp.net;

public interface OnNetListener<T> {
 public void onSuccess(T t);
    public void onFailure(Exception e);
}
 //App页面ImageLoder加载图片

package mvpframework.bwie.com.myshopapp.net;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration imageLoaderConfiguration=new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(imageLoaderConfiguration);
    }
}
//Model层
//model接口 
public interface IMainModel {
    public void getShops(OnNetListener<ShopBean> onNetListener);
}
model继承接口
public class MainModel implements IMainModel{
  private Handler handler=new Handler(Looper.getMainLooper());
    @Override
    public void getShops(final OnNetListener<ShopBean> onNetListener) {
        HttpUtils.getHttpUtils().doGet(Api.url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
onNetListener.onFailure(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
           String string=response.body().string();
                final ShopBean shopBean=new Gson().fromJson(string,ShopBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(shopBean);
                    }
                });
            }
        });
    }
}
//MVP  presenter实现方法
public class MainPresenter {
    private final IMainModel iMainModel;
   private IShopAppActivity iShopAppActivity;
    public MainPresenter(IShopAppActivity iShopAppActivity) {
        this.iShopAppActivity=iShopAppActivity;
        iMainModel=new MainModel();
    }


    public void getShops(){
        iMainModel.getShops(new OnNetListener<ShopBean>() {
            @Override
            public void onSuccess(ShopBean shopBean) {
                List<ShopBean.DataBean> dataBean=shopBean.getData();
                   List<List<ShopBean.DataBean.ListBean>> childList=
                           new ArrayList<List<ShopBean.DataBean.ListBean>>();
                   for(int i=0;i<dataBean.size();i++){
                       List<ShopBean.DataBean.ListBean> listBeen = dataBean.get(i).getList();
                       childList.add(listBeen);
               }
                iShopAppActivity.showList(dataBean,childList);
            }

            @Override
            public void onFailure(Exception e) {
              e.printStackTrace();
            }
        });
    }
}
//MVP view层接口
 
public interface IShopAppActivity {
    public void showList(List<ShopBean.DataBean> groupList,List<List<ShopBean.DataBean.ListBean>> childList);
}
//MVP  view层继承接口
public class ShopAppActivity extends AppCompatActivity implements IShopAppActivity {

    private ExpandableListView mElv;
    private CheckBox mCheckbox2;
    private TextView mPriceTv;
    private TextView mNumTv;
    private  MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_shop_app);
        initView();
        new MainPresenter(this).getShops();
        mCheckbox2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter.changeAllListCbState(mCheckbox2.isChecked());
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
    private void initView() {
        mElv = (ExpandableListView) findViewById(R.id.elv);
        mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2);
        mPriceTv = (TextView) findViewById(R.id.tv_price);
        mNumTv = (TextView) findViewById(R.id.tv_num);
    }

    @Override
    public void showList(List<ShopBean.DataBean> groupList, List<List<ShopBean.DataBean.ListBean>> childList) {
        adapter=new MyAdapter(this,groupList,childList);
        mElv.setAdapter(adapter);
        mElv.setGroupIndicator(null);
        //默认让其全部展开
        for(int i=0;i<groupList.size();i++){
            mElv.expandGroup(i);
        }

    }
    @Subscribe
  public void onMessageEvent(MessageEvent event){
   mCheckbox2.setChecked(event.isChecked());
  }
    @Subscribe
    public void onMessageEvent(PriceAndCountEvent event){
        mNumTv.setText("结算("+event.getCount()+")");
        mPriceTv.setText(event.getPrice() + "");
    }
}
//eventbusevent 购物车连接主页面的功能;
//MessageEvent
public class MessageEvent {
    private boolean checked;

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}
//PriceAndCountEvent
public class PriceAndCountEvent {
    private int price;
    private int count;

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
//Bean要加两个方法

//private boolean checked;
//public boolean isChecked() {
 //   return checked;
//}

//public void setChecked(boolean checked) {
  //  this.checked = checked;
//}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值