仿京东详情页跳转到购物车

代码名称与建包规范

      

依赖:

compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.2'
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.facebook.fresco:fresco:1.5.0'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
环境application

applicationId "com.bwie.a1510d_yuekao01"

SplashActivity
public class SplashActivity extends AppCompatActivity {
    private ProgressBarView pbv;
    private int progress = 120;
    private int time = 3;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //动画运行时间为3秒钟,动画结束后跳转到商品详情页面。
            time--;
            if (time == 0) {
                startActivity(new Intent(SplashActivity.this, SecondActivity.class));
                finish();
            } else {
                //设置动画播放进程
                progress += 120;
                pbv.setProgress(progress);
                handler.sendEmptyMessageDelayed(0, 1000);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        //查找控件
        ImageView imageView = (ImageView) findViewById(R.id.logo_img);
        pbv = (ProgressBarView) findViewById(R.id.my_progess);
        setAnimation(imageView);
        handler.sendEmptyMessage(0);
        pbv.setProgress(progress);
    }

    //执行动画的方法
    private void setAnimation(ImageView imageView) {
        //应用图标从屏幕最上方平移到屏幕中间
        ObjectAnimator trans = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 500f).setDuration(1000);
        //缩放由2倍到1倍
        ObjectAnimator scalX = ObjectAnimator.ofFloat(imageView, "scaleX", 2f, 1f).setDuration(1000);
        ObjectAnimator scalY = ObjectAnimator.ofFloat(imageView, "scaleY", 2f, 1f).setDuration(1000);
        //渐变从完全透明到完全不透明
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.0f, 1f).setDuration(1000);
        // 旋转为旋转一圈
        ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f).setDuration(1000);

        //动画组合开始执行
        AnimatorSet setAnimatior = new AnimatorSet();
        setAnimatior.play(trans).before(scalX).before(scalY).before(alpha).before(rotate);
        setAnimatior.start();
    }
}

ProgressBarView继承自定义View

public class ProgressBarView extends View {
    private Paint paint;
    private int currentX = 100;
    private int  currentY = 100;
    private int count;
    private PointF pointF = new PointF(currentX,currentY);
    private  int mProgress;


    public ProgressBarView(Context context) {
        super(context);
        initpaint(context);
    }

    private void initpaint(Context context) {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
    }

    public ProgressBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initpaint(context);
    }

    public ProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initpaint(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStrokeWidth(0);
        paint.setColor(Color.BLACK);
        canvas.drawCircle(pointF.x,pointF.y,20,paint);
        canvas.drawCircle(pointF.x,pointF.y,30,paint);

        paint.setStrokeWidth(10);
        paint.setColor(Color.RED);
        RectF recyF = new RectF(75,75,125,125);
        canvas.drawArc(recyF,-90,mProgress,false,paint);

        paint.setStrokeWidth(1);
        paint.setColor(Color.BLUE);
        canvas.drawText(count+"",98,102,paint);
    }

    public void setProgress(int progress){
        this.mProgress = progress;
        if (mProgress == 120){
            count = 2;
        }
        if (mProgress == 240){
            count = 1;
        }
        if (mProgress == 360){
            count = 0;
        }
        invalidate();
    }

}
MyApp 继承 Application

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this.getApplicationContext());
    }
}
Api

public interface Api {
    public static String ONE = "http://120.27.23.105/product/getProductDetail?pid=71&source=android";
    public static String TWO = "https://www.zhaoapi.cn/product/getCarts";
}
HttpUtils

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);
    }

    /**
     * post请求
     *
     * @param url      请求地址
     * @param params   请求参数
     * @param callback 回调
     */
    public void doPost(String url, Map<String, String> params, Callback callback) {
        //这里可以加网络判断

        /*//判断参数
        if (params == null || params.size() ==0){
            //运行时异常
            throw new RuntimeException("params is null!!!");
        }*/
        //创建Request
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }
        FormBody formBody = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        client.newCall(request).enqueue(callback);
    }
}
public interface OnNetLisenter<T> {
    public void onSuccess(T t);

    public void onFailure(Exception e);
}
SecondModel

public class SecondModel implements ISecondModel {
    private Handler handler = new Handler(Looper.getMainLooper());

    @Override
    public void getDetail(final OnNetLisenter<SecondBean> onNetLisenter) {
        HttpUtils.getHttpUtils().doGet(Api.ONE, new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetLisenter.onFailure(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        SecondBean secondBean = new Gson().fromJson(string, SecondBean.class);
                        onNetLisenter.onSuccess(secondBean);
                    }
                });
            }
        });
    }
}
public interface ISecondModel {
    public void getDetail(final OnNetLisenter<SecondBean> onNetLisenter);
}

P层
public class SecondPresenter {
    private ISecondModel iSecondModel;
    private IScondActivity iScondActivity;

    public SecondPresenter(IScondActivity iScondActivity) {
        this.iScondActivity = iScondActivity;
        this.iSecondModel = new SecondModel();
    }



    public void getDetail(){
        iSecondModel.getDetail(new OnNetLisenter<SecondBean>() {
            @Override
            public void onSuccess(SecondBean secondBean) {
                if (iScondActivity != null){
                    iScondActivity.show(secondBean.getData());
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }
}
public class SecondActivity extends AppCompatActivity implements View.OnClickListener, IScondActivity {

    private ImageView mBackImage;
    private RelativeLayout mRelative01;
    private View mView;
    private ImageView mProductImage;
    private RelativeLayout mRelative;
    private View mView01;
    private TextView mTitle;
    private TextView mYuanJia;
    private TextView mYouHui;
    private LinearLayout mLine1;
    /**
     * 购物车
     */
    private Button mGoToCart;
    /**
     * 加入购物车
     */
    private Button mAddCart;
    private LinearLayout mLine2;
    private SecondPresenter secondPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initView();
        secondPresenter = new SecondPresenter(this);
        secondPresenter.getDetail();
    }

    private void initView() {
        mBackImage = (ImageView) findViewById(R.id.backImage);
        mRelative01 = (RelativeLayout) findViewById(R.id.relative01);
        mView = (View) findViewById(R.id.view);
        mProductImage = (ImageView) findViewById(R.id.ProductImage);
        mRelative = (RelativeLayout) findViewById(R.id.relative);
        mView01 = (View) findViewById(R.id.view01);
        mTitle = (TextView) findViewById(R.id.title);
        mYuanJia = (TextView) findViewById(R.id.yuanJia);
        mYouHui = (TextView) findViewById(R.id.youHui);
        mLine1 = (LinearLayout) findViewById(R.id.line1);
        mGoToCart = (Button) findViewById(R.id.goToCart);
        mGoToCart.setOnClickListener(this);
        mAddCart = (Button) findViewById(R.id.addCart);
        mAddCart.setOnClickListener(this);
        mLine2 = (LinearLayout) findViewById(R.id.line2);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.backImage:
                finish();
                break;
            case R.id.goToCart:
                Intent intent1 = new Intent(SecondActivity.this, ThirdActivity.class);
                startActivity(intent1);
                break;
            case R.id.addCart:
                //路径
                String path = "https://www.zhaoapi.cn/product/addCart?uid=71&pid=1";
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(path)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String body = response.body().string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //吐司加入购物车成功
                                Toast.makeText(SecondActivity.this, "购物车加入商品成功" + body, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
                break;
        }
    }


    @Override
    public void show(SecondBean.DataBean secondBean) {
        String imgs = secondBean.getImages();
        String[] split = imgs.split("\\|");
        Glide.with(SecondActivity.this).load(split[0]).into(mProductImage);
        //设置商品信息显示
        mTitle.setText(secondBean.getTitle());
        mYuanJia.setText("原价:¥" + secondBean.getPrice());
        //设置原价中间横线(删除线)
        mYuanJia.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        mYouHui.setText("优惠价:" + secondBean.getBargainPrice());
    }
public interface IScondActivity {
    public void show(SecondBean.DataBean secondBean);
}
XML布局

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="58dp"
    android:id="@+id/relative01">

    <ImageView
        android:padding="5dp"
        android:id="@+id/backImage"
        android:layout_width="38dp"
        android:layout_height="38dp"
        android:src="@drawable/leftjiantou"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"/>

    <TextView
        android:padding="10dp"
        android:text="商品详情"
        android:textSize="26sp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"    />
</RelativeLayout>
<View
    android:id="@+id/view"
    android:background="#000"
    android:layout_height="1dp"
    android:visibility="visible"
    android:layout_width="match_parent"
    android:layout_below="@+id/relative01"></View>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_below="@+id/view"
    android:layout_height="288dp"
    android:id="@+id/relative">

    <ImageView
        android:id="@+id/ProductImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
<View
    android:id="@+id/view01"
    android:background="#000"
    android:layout_height="1dp"
    android:visibility="visible"
    android:layout_width="match_parent"
    android:layout_below="@+id/relative"></View>
<LinearLayout
    android:id="@+id/line1"
    android:orientation="vertical"
    android:layout_above="@+id/line2"
    android:layout_width="match_parent"
    android:layout_below="@+id/relative"
    android:layout_height="wrap_content"  >
    <TextView
        android:textSize="18sp"
        android:id="@+id/title"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:layout_marginLeft="18dp"
        android:layout_width="wrap_content"    />
    <TextView
        android:textSize="18sp"
        android:id="@+id/yuanJia"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:layout_marginLeft="18dp"
        android:layout_width="wrap_content"     />
    <TextView
        android:textSize="18sp"
        android:id="@+id/youHui"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:textColor="#f14d07"
        android:layout_marginLeft="18dp"
        android:layout_width="wrap_content"    />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_above="@+id/line2"
    android:visibility="visible"
    android:background="#000"
    android:layout_height="1dp"></View>

<LinearLayout
    android:id="@+id/line2"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">
    <Button
        android:text="购物车"
        android:gravity="center"
        android:id="@+id/goToCart"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/buttonstyle" ></Button>
    <Button
        android:gravity="center"
        android:text="加入购物车"
        android:id="@+id/addCart"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:background="@drawable/buttonstyle"
        android:layout_height="wrap_content"></Button>
</LinearLayout>
ThridModel

public class ThirdModel implements IThirdModel{
    private Handler handler = new Handler(Looper.getMainLooper());
    @Override
    public void getBean(Map<String, String> params, final OnNetLisenter<ThirdBean> onNetLisenter) {
        HttpUtils.getHttpUtils().doPost(Api.TWO, params, new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetLisenter.onFailure(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ThirdBean thirdBean = new Gson().fromJson(string,ThirdBean.class);
                        onNetLisenter.onSuccess(thirdBean);
                    }
                });
            }
        });
    }
}
public interface IThirdModel {
    public void getBean(Map<String,String> params,OnNetLisenter<ThirdBean> onNetLisenter);
}
p层

public class ThirdPresenter {
    private IThirdModel iThirdModel;
    private IThirdActivity iThirdActivity;

    public ThirdPresenter(IThirdActivity iThirdActivity) {
        this.iThirdActivity = iThirdActivity;
        iThirdModel = new ThirdModel();
    }

    public void getBean(String uid) {
        Map<String, String> params = new HashMap<>();
        params.put("uid", "71");

        params.put("source", "android");
        iThirdModel.getBean(params, new OnNetLisenter<ThirdBean>() {
            @Override
            public void onSuccess(ThirdBean thirdBean) {
                List<List<ThirdBean.DataBean.ListBean>> child = new ArrayList<>();
                for (int i = 0; i < thirdBean.getData().size(); i++) {
                    child.add(thirdBean.getData().get(i).getList());
                }
                if (iThirdActivity != null) {
                    iThirdActivity.show(thirdBean.getData(), child);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }

        });

    }
}
ThridActivity

public class ThirdActivity extends AppCompatActivity implements IThirdActivity{

    private ExpandableListView mElv;
    /**
     * 全选
     */
    private CheckBox mCb;
    /**
     * 合计:
     */
    private TextView mTvTotal;
    /**
     * 去结算(0)
     */
    private TextView mTvCount;
    private ElvAdapter adapter;
    private ThirdPresenter thirdPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        initView();
        thirdPresenter = new ThirdPresenter(this);
        thirdPresenter.getBean(getSharedPreferences("user", Context.MODE_PRIVATE).getString("uid", "71"));


        //给全选设置点击事件
        mCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapter != null) {
                    adapter.setAllChecked(mCb.isChecked());
                }
            }
        });
    }

    private void initView() {
        mElv = (ExpandableListView) findViewById(R.id.elv);
        mElv.setGroupIndicator(null);
        mCb = (CheckBox) findViewById(R.id.cb);
        mTvTotal = (TextView) findViewById(R.id.tvTotal);
        mTvCount = (TextView) findViewById(R.id.tvCount);
    }

    @Override
    public void show(List<ThirdBean.DataBean> group, List<List<ThirdBean.DataBean.ListBean>> child) {
        //设置适配器
        adapter = new ElvAdapter(this, group, child);
        mElv.setAdapter(adapter);

        for (int i = 0; i < group.size(); i++) {
            mElv.expandGroup(i);
        }
    }
    public void setMoney(double price) {
        mTvTotal.setText("合计:" + price);
    }

    public void setCount(int count) {
        mTvCount.setText("去结算(" + count + ")");
    }

    public void setAllSelect(boolean bool) {
        mCb.setChecked(bool);
    }
}
public interface IThirdActivity {
    public void show(List<ThirdBean.DataBean> group, List<List<ThirdBean.DataBean.ListBean>> child);
}
购物车适配器

public class ElvAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<ThirdBean.DataBean> group;
    private List<List<ThirdBean.DataBean.ListBean>> child;
    private LayoutInflater inflater;

    public ElvAdapter(Context context, List<ThirdBean.DataBean> group, List<List<ThirdBean.DataBean.ListBean>> child) {
        this.context = context;
        this.group = group;
        this.child = child;
        inflater = LayoutInflater.from(context);
    }


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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child.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, View convertView, ViewGroup parent) {
        View view;
        final GroupViewHolder holder;
        if (convertView == null) {
            view = inflater.inflate(R.layout.elv_group, null);
            holder = new GroupViewHolder();
            holder.tv = view.findViewById(R.id.tvGroup);
            holder.cbGroup = view.findViewById(R.id.cbGroup);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (GroupViewHolder) view.getTag();
        }
        final ThirdBean.DataBean dataBean = group.get(groupPosition);
        holder.tv.setText(dataBean.getSellerName());
        holder.cbGroup.setChecked(dataBean.isChecked());


        holder.cbGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataBean.setChecked(holder.cbGroup.isChecked());
                //改变子列表中的所有checkbox的状态
                setChildsChecked(groupPosition, holder.cbGroup.isChecked());
                //改变全选状态
                ((ThirdActivity) context).setAllSelect(isGroupCbChecked());
                //计算钱和数量
                setPriceAndCount();
                //刷新列表
                notifyDataSetChanged();
            }
        });

        return view;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view;
        final ChildViewHolder holder;
        if (convertView == null) {
            view = inflater.inflate(R.layout.elv_child, null);
            holder = new ChildViewHolder();
            holder.iv = view.findViewById(R.id.iv);
            holder.tvTitle = view.findViewById(R.id.tvTitle);
            holder.tvSubhead = view.findViewById(R.id.tvSubhead);
            holder.tvPrice = view.findViewById(R.id.tvPrice);
            holder.cbChild = view.findViewById(R.id.cbChild);
            holder.btDel = view.findViewById(R.id.btDel);
            holder.tvNum = view.findViewById(R.id.tvNum);
            holder.ivDel = view.findViewById(R.id.ivDel);
            holder.ivAdd = view.findViewById(R.id.ivAdd);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ChildViewHolder) view.getTag();
        }
        final ThirdBean.DataBean.ListBean listBean = child.get(groupPosition).get(childPosition);
        String images = listBean.getImages();
        Glide.with(context).load(images.split("\\|")[0]).into(holder.iv);
        holder.tvTitle.setText(listBean.getTitle());
        holder.cbChild.setChecked(child.get(groupPosition).get(childPosition).isChecked());
        holder.tvSubhead.setText(listBean.getSubhead());
        holder.tvPrice.setText(listBean.getPrice() + "元");
        holder.tvNum.setText(listBean.getCount() + "");

        holder.btDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除
                setPriceAndCount();
                child.get(groupPosition).remove(childPosition);
                if (child.get(groupPosition).size() == 0) {
                    child.remove(groupPosition);
                    group.remove(groupPosition);
                }
                notifyDataSetChanged();

            }
        });

        //给减号设置点击事件
        holder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int c = listBean.getCount();
                if (c <= 1) {
                    c = 1;
                } else {
                    c--;
                }
                setNum(listBean, holder, c);
            }
        });
        //给加号设置点击事件
        holder.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int c = listBean.getCount();
                c++;
                setNum(listBean, holder, c);
            }
        });

        //给子列表的checkbox设置点击事件
        holder.cbChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击checkbox的时候,给对应的属性值设置true或者false
                listBean.setChecked(holder.cbChild.isChecked());
                group.get(groupPosition).setChecked(isChildsCbChecked(groupPosition));
                //如果某个一级列表下的二级列表全部选中时,则要判断其它的一级列表是否都选中,去改变“全选”状态
                ((ThirdActivity) context).setAllSelect(isGroupCbChecked());
                setPriceAndCount();
                //刷新页面
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupViewHolder {
        TextView tv;
        CheckBox cbGroup;
    }

    class ChildViewHolder {
        ImageView iv;
        TextView tvTitle;
        TextView tvSubhead;
        TextView tvPrice;
        CheckBox cbChild;
        Button btDel;
        TextView tvNum;
        ImageView ivDel;
        ImageView ivAdd;
    }

    private void setNum(ThirdBean.DataBean.ListBean listBean, ChildViewHolder holder, int c) {
        //改变bean里的那个值
        listBean.setCount(c);
        holder.tvNum.setText(c + "");
        //计算钱和数量,并显示
        setPriceAndCount();
        //刷新页面
        notifyDataSetChanged();
    }

    /**
     * 改变子列表中的所有checkbox的状态
     *
     * @param groupPosition
     * @param bool
     */
    private void setChildsChecked(int groupPosition, boolean bool) {
        List<ThirdBean.DataBean.ListBean> listBeans = child.get(groupPosition);
        for (int i = 0; i < listBeans.size(); i++) {
            ThirdBean.DataBean.ListBean listBean = listBeans.get(i);
            listBean.setChecked(bool);
        }
    }

    /**
     * 判断一级列表是否全部选中
     *
     * @return
     */
    private boolean isGroupCbChecked() {
        if (group.size() == 0) {
            return false;
        }
        for (int i = 0; i < group.size(); i++) {
            ThirdBean.DataBean dataBean = group.get(i);
            if (!dataBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断同一商家下的子列表checkbox是否都选中
     *
     * @param groupPosition
     * @return
     */
    private boolean isChildsCbChecked(int groupPosition) {
        List<ThirdBean.DataBean.ListBean> listBeans = child.get(groupPosition);
        for (int i = 0; i < listBeans.size(); i++) {
            ThirdBean.DataBean.ListBean listBean = listBeans.get(i);
            if (!listBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 设置钱和数量
     */
    private void setPriceAndCount() {
        PriceAndCount priceAndCount = computePrice();
        ((ThirdActivity) context).setMoney(priceAndCount.getPrice());
        ((ThirdActivity) context).setCount(priceAndCount.getCount());
    }

    /**
     * 计算钱
     */
    private PriceAndCount computePrice() {
        double sum = 0;
        int count = 0;
        for (int i = 0; i < child.size(); i++) {
            List<ThirdBean.DataBean.ListBean> listBeans = child.get(i);
            for (int j = 0; j < listBeans.size(); j++) {
                ThirdBean.DataBean.ListBean listBean = listBeans.get(j);
                //表示是否选中
                if (listBean.isChecked()) {
                    count += listBean.getCount();
                    sum += listBean.getPrice() * listBean.getCount();
                }

            }
        }
        return new PriceAndCount(sum, count);
    }


    /**
     * “全选”改变状态
     *
     * @param bool
     */
    public void setAllChecked(boolean bool) {
        for (int i = 0; i < group.size(); i++) {
            ThirdBean.DataBean dataBean = group.get(i);
            dataBean.setChecked(bool);
            setChildsChecked(i, bool);
        }
        setPriceAndCount();
        //刷新页面
        notifyDataSetChanged();

    }
}
PriceAndCount

public class PriceAndCount {
    private double price;
    private int count;

    public PriceAndCount(double price, int count) {
        this.price = price;
        this.count = count;
    }

    public double getPrice() {
        return price;
    }

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

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}


xml布局

<ImageView
    android:id="@+id/logo_img"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@mipmap/ic_launcher"
    android:layout_centerHorizontal="true" />

<com.bwie.a1510d_yuekao01.ProgressBarView
    android:visibility="gone"
    android:id="@+id/my_progess"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#ff3660"
    android:gravity="center"
    android:text="购物车"
    android:textSize="30sp" />


<ExpandableListView
    android:id="@+id/elv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="40dp">

    <CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="全选" />

    <TextView
        android:id="@+id/tvTotal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@id/cb"
        android:text="合计:" />

    <TextView
        android:id="@+id/tvCount"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="#ff0000"
        android:gravity="center"
        android:text="去结算(0)"
        android:textColor="#ffffff" />
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingLeft="50dp">

    <CheckBox
        android:id="@+id/cbChild"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/iv"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tvSubhead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tvSubhead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tvPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:layout_marginLeft="10dp"
                android:id="@+id/ivDel"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:background="@drawable/iv_del" />

            <TextView
                android:id="@+id/tvNum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="25sp"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:text="1"/>

            <ImageView
                android:id="@+id/ivAdd"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:background="@drawable/iv_add" />
        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/btDel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/cbGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvGroup"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical" />
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值