依赖
//MetrialDesign:Design库
implementation 'com.android.support:design:28.+'
//Butterknife:根据反射注入框架
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//Brvah:RecyclerView快速开发框架
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
//SmartRefreshLayout
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-19'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-19'
//没有使用特殊Header,可以不加这行
//okgo
//必须使用
implementation 'com.lzy.net:okgo:3.0.4'
implementation 'com.google.code.gson:gson:2.8.5'
//图片加载框架
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
//activity
public class ShopActivity extends AppCompatActivity implements IShopContract.IShopView, View.OnClickListener {
@BindView(R.id.m_recycler)
RecyclerView mRecycler;
@BindView(R.id.m_clok)
CheckBox mClok;
@BindView(R.id.m_btn_price)
Button mBtnPrice;
@BindView(R.id.m_tv_count)
TextView mTvCount;
private ShopPresenter presenter;
private List<ShopBean.DataBean> dataBean;
private Context mContext;
private MyQnameBaseAdapter qnameBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mContext = ShopActivity.this;
presenter = new ShopPresenter();
presenter.attachView(this);
//动作
presenter.requestData();
}
@Override
public void showLoading() {
}
@Override
public void hideLoading() {
}
@Override
public void showData(String urlString) {
//
mClok.setOnCheckedChangeListener(null);
//全选处理
mClok.setOnClickListener(this);
//解析
Gson gson = new Gson();
ShopBean shopBean = gson.fromJson(urlString, ShopBean.class);
dataBean = shopBean.getData();
//设置布局管理器
LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
mRecycler.setLayoutManager(manager);
//设置适配器
qnameBaseAdapter = new MyQnameBaseAdapter(R.layout.item_name_sj, dataBean);
mRecycler.setAdapter(qnameBaseAdapter);
//
qnameBaseAdapter.setOnNameClickLisenter(new MyQnameBaseAdapter.OnNameClickLisenter() {
@Override
public void onCallBack() {
boolean reslt = true;
for (int i = 0; i < dataBean.size(); i++) {
//外层选中状态
boolean nameCheckedd = dataBean.get(i).getNameCheckedd();
reslt = reslt & nameCheckedd;
for (int j = 0; j < dataBean.get(i).getList().size(); j++) {
//内层选中状态
boolean goodsChecked = dataBean.get(i).getList().get(j).getGoodsChecked();
reslt = reslt & goodsChecked;
}
}
mClok.setChecked(reslt);
//计算价格
calcuCount();
}
});
}
private void calcuCount() {
double totCount = 0;
for (int i = 0; i < dataBean.size(); i++) {
for (int j = 0; j < dataBean.get(i).getList().size(); j++) {
//判断内层条目是否勾选
if (dataBean.get(i).getList().get(j).getGoodsChecked()){
double price = dataBean.get(i).getList().get(j).getPrice();
int sumTet = dataBean.get(i).getList().get(j).getSumTet();
double goodsprice = price * sumTet;
totCount = totCount + goodsprice;
}
}
}
mTvCount.setText("总价是:"+String.valueOf(totCount));
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detachView(this);
}
//全选点击触发事件
@Override
public void onClick(View v) {
//全选处理
for (int i = 0; i < dataBean.size(); i++) {
dataBean.get(i).setNameChecked(mClok.isChecked());
for (int j = 0; j < dataBean.get(i).getList().size(); j++) {
dataBean.get(i).getList().get(j).setGoodsChecked(mClok.isChecked());
}
}
//适配器刷新
qnameBaseAdapter.notifyDataSetChanged();
//计算价格
calcuCount();
}
}
//activity 对应的Xml布局
<?xml version="1.0" encoding="utf-8"?>
<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=".ui.activity.ShopActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:fitsSystemWindows="true"
app:title="@string/toolbar_name" />
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v7.widget.RecyclerView
android:id="@+id/m_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/m_clok"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="全选" />
<Button
android:id="@+id/m_btn_price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="结算" />
<TextView
android:id="@+id/m_tv_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:layout_toLeftOf="@+id/m_btn_price"
android:gravity="center_vertical"
android:text="总计:0元" />
</RelativeLayout>
</LinearLayout>
//商家条目 : Adapter
public class MyQnameBaseAdapter extends BaseQuickAdapter<ShopBean.DataBean,BaseViewHolder> {
/*____________接口回调________*/
OnNameClickLisenter nameClickLisenter;
public interface OnNameClickLisenter{
public void onCallBack();
}
public void setOnNameClickLisenter(OnNameClickLisenter nameClickLisenter){
this.nameClickLisenter = nameClickLisenter;
}
/*____________________________*/
public MyQnameBaseAdapter(int layoutResId, @Nullable List<ShopBean.DataBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, final ShopBean.DataBean item) {
helper.setText(R.id.sj_name,item.getSellerName());
//商品条目 控件
RecyclerView goodsRecyclerView = helper.getView(R.id.sj_rv_goods);
//避免焦点抢占
final CheckBox nameCheckBox = helper.getView(R.id.sj_ck_xz);
nameCheckBox.setOnCheckedChangeListener(null);
//获取商家条目是否选中状态
nameCheckBox.setChecked(item.getNameCheckedd());
//数据
List<ShopBean.DataBean.ListBean> listBeans = item.getList();
//设置布局管理器
LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
goodsRecyclerView.setLayoutManager(manager);
//设置适配器
final MyGoodsAdapter goodsAdapter = new MyGoodsAdapter(R.layout.item_goods_sp,listBeans);
goodsRecyclerView.setAdapter(goodsAdapter);
//商品条目控制 商家条目
goodsAdapter.setOnGoodsClickLisenter(new MyGoodsAdapter.OnGoodsClickLisenter() {
@Override
public void onCallBack() {
boolean reslt = true;
//遍历获取所有子条目,只要有一个未勾选,商品类别也应该是未勾选
for (int i = 0; i < item.getList().size(); i++) {
boolean goodsChecked = item.getList().get(i).getGoodsChecked();
reslt = reslt & goodsChecked;
}
nameCheckBox.setChecked(reslt);
//刷新
goodsAdapter.notifyDataSetChanged();
//回传状态
nameClickLisenter.onCallBack();
}
});
//商家条目控制商品天目
nameCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < item.getList().size(); i++) {
item.getList().get(i).setGoodsChecked(nameCheckBox.isChecked());
}
item.setNameChecked(nameCheckBox.isChecked());
//刷新
notifyDataSetChanged();
//回传状态
nameClickLisenter.onCallBack();
}
});
}
}
//商家条目 对应的Xml布局
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
tools:context=".ui.activity.ShopActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/sj_ck_xz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:id="@+id/sj_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:paddingLeft="5dp"
android:text="店名"
android:textSize="22sp" />
</LinearLayout>r
<android.support.v7.widget.RecyclerView
android:id="@+id/sj_rv_goods"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
//商品条目 Adapter
public class MyGoodsAdapter extends BaseQuickAdapter<ShopBean.DataBean.ListBean,BaseViewHolder> {
/*____________接口回调________*/
OnGoodsClickLisenter goodsClickLisenter;
public interface OnGoodsClickLisenter{
public void onCallBack();
}
public void setOnGoodsClickLisenter(OnGoodsClickLisenter goodsClickLisenter){
this.goodsClickLisenter = goodsClickLisenter;
}
/*____________________________*/
public MyGoodsAdapter(int layoutResId, @Nullable List<ShopBean.DataBean.ListBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, final ShopBean.DataBean.ListBean item) {
helper.setText(R.id.goods_name,item.getTitle());
helper.setText(R.id.goods_price,"$:"+item.getPrice());
//获取控件
ImageView iv_goodsIcon = helper.getView(R.id.sp_goods_image);
//获取url值并剪切
String imagesString = item.getImages();
String[] imagesStr = imagesString.split("\\|");
//给控件图片
Glide.with(mContext).load(imagesStr[0]).into(iv_goodsIcon);
//避免焦点抢占
CheckBox cb_goods = helper.getView(R.id.cb_goods);
cb_goods.setOnCheckedChangeListener(null);
cb_goods.setChecked(item.getGoodsChecked());
//使用接口把状态回传
cb_goods.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
item.setGoodsChecked(isChecked);
goodsClickLisenter.onCallBack();
}
});
//获取自定义Viwe控件
CalculatorView calculatorView = helper.getView(R.id.sp_price_cal);
//
calculatorView.setOnCalculatorLisenter(new CalculatorView.OnCalculatorLisenter() {
@Override
public void OnJian(int number) {
//对新增的字段进行改动
item.setSumTet(number);
goodsClickLisenter.onCallBack();
}
@Override
public void OnAdd(int number) {
//对新增的字段进行改动
item.setSumTet(number);
goodsClickLisenter.onCallBack();
}
});
}
}
//商品条目 对应的xml布局
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
tools:context=".ui.activity.ShopActivity"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="@dimen/dp_10"
app:cardCornerRadius="@dimen/dp_4"
app:contentPaddingBottom="@dimen/dp_4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp">
<CheckBox
android:id="@+id/cb_goods"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_centerVertical="true" />
<ImageView
android:id="@+id/sp_goods_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_toRightOf="@id/cb_goods"
android:paddingLeft="5dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_toRightOf="@id/sp_goods_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<TextView
android:id="@+id/goods_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:hint="商品名称"
android:maxEms="20"
android:paddingLeft="5dp"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:id="@+id/goods_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/goods_name"
android:layout_marginTop="@dimen/dp_10"
android:hint="商品价格" />
<gxq.com.shopping001.ui.widget.CalculatorView
android:id="@+id/sp_price_cal"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content"></gxq.com.shopping001.ui.widget.CalculatorView>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
//自定义View加减器
Xml布局
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:layout_height="wrap_content"
tools:context=".ui.activity.ShopActivity"
android:orientation="horizontal">
<Button
android:id="@+id/but_jian"
android:layout_width="@dimen/dp_40"
android:layout_height="@dimen/dp_40"
android:padding="@dimen/dp_10"
android:background="@mipmap/decrease"/>
<TextView
android:id="@+id/tv_numb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:inputType="number"
android:text="1"/>
<Button
android:id="@+id/but_jia"
android:layout_width="@dimen/dp_40"
android:layout_height="@dimen/dp_40"
android:padding="@dimen/dp_10"
android:background="@mipmap/add"/>
</LinearLayout>
//实现
public class CalculatorView extends LinearLayout implements View.OnClickListener {
private final Button but_jian;
private final Button but_jia;
private final TextView tv_numb;
public CalculatorView(Context context, AttributeSet attrs) {
super(context, attrs);
View rootView = LayoutInflater.from(context).inflate(R.layout.sp_jia_jian, this);
but_jian = rootView.findViewById(R.id.but_jian);
but_jia = rootView.findViewById(R.id.but_jia);
tv_numb = rootView.findViewById(R.id.tv_numb);
////
but_jian.setOnClickListener(this);
but_jia.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String numberString = tv_numb.getText().toString();
int number = Integer.parseInt(numberString);
switch (v.getId()){
case R.id.but_jian:
number = number - 1;
if(number<0){
number=0;
// 最小数量为0
tv_numb.setText(String.valueOf(number));
}
//赋值
tv_numb.setText(String.valueOf(number));
//接口回调
calculatorLisenter.OnJian(number);
break;
case R.id.but_jia:
number = number + 1;
//赋值
tv_numb.setText(String.valueOf(number));
//接口回调
calculatorLisenter.OnAdd(number);
break;
}
}
/*________ 接口回调 ______*/
OnCalculatorLisenter calculatorLisenter;
public interface OnCalculatorLisenter{
public void OnJian(int number);
public void OnAdd(int number);
}
public void setOnCalculatorLisenter(OnCalculatorLisenter calculatorLisenter){
this.calculatorLisenter = calculatorLisenter;
}
/*_____________________*/
}
//效果图