Activity
public class GwCActivity extends AppCompatActivity implements DataCall {
private ExpandableListView mExpandActivitiesButton;
/**
* 全选
*/
private CheckBox mCheckbox;
/**
* 总价:
*/
private TextView mZongjia;
private List<Meau.DataBean> list = new ArrayList<>();
private MyExcptionAdapter myExcptionAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gw_c);
initView();
String url = "http://www.zhaoapi.cn/product/getCarts?uid=71";
RequestPresenter requestPresenter = new RequestPresenter(this);
requestPresenter.getData(url);
myExcptionAdapter = new MyExcptionAdapter(list, this);
mExpandActivitiesButton.setAdapter(myExcptionAdapter);
mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
myExcptionAdapter.checkAll(isChecked);
}
});
}
@Override
public void requestSuccess(Meau meau) {
List<Meau.DataBean> data = meau.getData();
list.addAll(data);
//遍历所有group,将所有项设置成默认展开
int size = meau.getData().size();
for (int i = 0; i < size; i++) {
mExpandActivitiesButton.expandGroup(i);
}
myExcptionAdapter.notifyDataSetChanged();
Toast.makeText(this, "成功" + meau.getData().toString(), Toast.LENGTH_LONG).show();
}
@Override
public void requestFail(Meau meau) {
Toast.makeText(this, "失败", Toast.LENGTH_LONG).show();
}
private void initView() {
mExpandActivitiesButton = findViewById(R.id.expand_activities_button);
mCheckbox = findViewById(R.id.checkbox);
mZongjia = findViewById(R.id.zongjia);
}
}
适配器
public class MyExcptionAdapter extends BaseExpandableListAdapter {
private List<Meau.DataBean> list = new ArrayList<>();
private Context context;
private TotalPriceListener totalPriceListener;
public MyExcptionAdapter(List<Meau.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.get(groupPosition).getList().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(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHodler holder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.cart2_group_item, null);
holder = new GroupHodler();
holder.checkBox = convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
} else {
holder = (GroupHodler) convertView.getTag();
}
holder.checkBox.setText(list.get(groupPosition).getSellerName());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
MyHolder holder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.cart_item, null);
holder = new MyHolder();
holder.text = convertView.findViewById(R.id.text);
holder.price = convertView.findViewById(R.id.text_price);
holder.image = convertView.findViewById(R.id.image);
holder.check = convertView.findViewById(R.id.cart_goods_check);
convertView.setTag(holder);
} else {
holder = (MyHolder) convertView.getTag();
}
holder.text.setText(list.get(groupPosition).getList().get(childPosition).getTitle());
holder.price.setText(list.get(groupPosition).getList().get(childPosition).getPrice() + "");
//截取图片地址
String images1 = list.get(groupPosition).getList().get(childPosition).getImages();
String[] split = images1.split("\\|");
if (split.length > 0) {
Glide.with(context).load(split[0]).into(holder.image);
}
if (list.get(groupPosition).getList().get(childPosition).getSelected() == 0) {
holder.check.setChecked(false);
} else {
holder.check.setChecked(true);
}
return convertView;
}
/**
* @author dingtao
* @date 2018/12/18 7:33 PM
* 全部选中或者取消
*/
public void checkAll(boolean isCheck) {
for (int i = 0; i < list.size(); i++) { //循环的商家
Meau.DataBean dataBean = list.get(i);
dataBean.setCheck(isCheck);
for (int j = 0; j < dataBean.getList().size(); j++) {
Meau.DataBean.ListBean listBean = dataBean.getList().get(j);
listBean.setSelected(isCheck ? 1 : 0);
}
}
notifyDataSetChanged();
}
/**
* @author dingtao
* @date 2018/12/18 7:01 PM
* 计算总价格
*/
private void calculatePrice(){
double totalPrice=0;
for (int i = 0; i < list.size(); i++) {//循环的商家
Meau.DataBean dataBean = list.get(i);
for (int j = 0; j < dataBean.getList().size(); j++) {
Meau.DataBean.ListBean listBean = dataBean.getList().get(j);
if (listBean.getSelected()==1) {//如果是选中状态
totalPrice = totalPrice + listBean.getNum() * listBean.getPrice();
}
}
}
if (totalPriceListener!=null)
totalPriceListener.totalPrice(totalPrice);
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class MyHolder {
CheckBox check;
TextView text;
TextView price;
ImageView image;
//自定义的view控件---------加减
MyView addSub;
}
class GroupHodler {
CheckBox checkBox;
}
public void setTotalPriceListener(TotalPriceListener totalPriceListener) {
this.totalPriceListener = totalPriceListener;
}
public interface TotalPriceListener{
void totalPrice(double totalPrice);
}
}
自定义View
public class MyView extends LinearLayout implements View.OnClickListener {
private TextView mAddBtn,mSubBtn;
private TextView mNumText;
private MyViewListener myViewListener;
public MyView(Context context) {
super(context);
initView();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView(){
//加载layout布局,第三个参数ViewGroup一定写成this
View view = View.inflate(getContext(),R.layout.car_add_sub_layout,this);
mAddBtn = view.findViewById(R.id.btn_add);
mSubBtn = view.findViewById(R.id.btn_sub);
mNumText = view.findViewById(R.id.text_number);
mAddBtn.setOnClickListener(this);
mSubBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int number = Integer.parseInt(mNumText.getText().toString());
switch (v.getId()){
case R.id.btn_add:
number++;
mNumText.setText(number+"");
break;
case R.id.btn_sub:
if (number==0){
Toast.makeText(getContext(),"数量不能小于0",Toast.LENGTH_LONG).show();
return;
}
number--;
mNumText.setText(number+"");
break;
}
if (myViewListener != null){
myViewListener.myView(number);
}
}
public void setCount(int count){
mNumText.setText(count+"");
}
public void setMyViewListener(MyViewListener myViewListener) {
this.myViewListener = myViewListener;
}
public interface MyViewListener{
void myView(int count);
}
}
自定义接口
public interface DataCall {
void requestSuccess(Meau meau);
void requestFail(Meau meau);
}
Presenter中解析数据
public class RequestPresenter {
private final DataCall dataCall;
private Handler handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
Meau meau = (Meau) msg.obj;
if (meau.getCode() == 0){
dataCall.requestSuccess(meau);
}else {
dataCall.requestFail(meau);
}
}
};
public RequestPresenter(DataCall dataCall) {
this.dataCall = dataCall;
}
public void getData(final String url){
new Thread(new Runnable() {
@Override
public void run() {
String s = Utils.get(url);
Gson gson = new Gson();
Meau meau = gson.fromJson(s, Meau.class);
Message message = handler.obtainMessage();
message.obj = meau;
handler.sendMessage(message);
}
}).start();
}
}
工具类
public class Utils {
public static String get(String url){
OkHttpClient okHttpClient = new OkHttpClient();
Request build = new Request.Builder().url(url).get().build();
try {
Response execute = okHttpClient.newCall(build).execute();
return execute.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
图片截图
//截取图片地址
String images = mListData.get(position).getImages();
String[] split = images.split("\\|");
if (split.length > 0) {
Glide.with(mContext).load(split[0]).into(holder.imageView);
}