主界面
<? version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout ns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:orientation=\"vertical\">
<TextView
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:background=\"@color/colorAccent\"
android:gravity=\"center\"
android:padding=\"10dp\"
android:text=\"购物车\"
android:textColor=\"#fff\"
android:textSize=\"25sp\" />
<ExpandableListView
android:id=\"@+id/list_cart\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:layout_weight=\"1\" />
<RelativeLayout
android:layout_width=\"match_parent\"
android:layout_height=\"50dp\">
<CheckBox
android:id=\"@+id/check_all\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_centerVertical=\"true\"
android:layout_marginLeft=\"10dp\"
android:text=\"全选\" />
<TextView
android:id=\"@+id/goods_sum_price\"
android:layout_toRightOf=\"@+id/check_all\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"价格:\"
android:layout_marginLeft=\"20dp\"
android:layout_centerVertical=\"true\"/>
</RelativeLayout>
</LinearLayout >
fragment这代码
public class two_frag extends Fragment implements CallBacks<List<Shop>>,CartAdapter2.TotalPriceListener{
private TextView mSumPrice;
private CheckBox mCheckAll;
private CartAdapter2 mCartAdapter;
private ExpandableListView mGoodsList;
private CartPresenter cartPresenter = new CartPresenter(this);
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.item_two,container,false);
mSumPrice=view.findViewById(R.id.goods_sum_price);
mCheckAll = view.findViewById(R.id.check_all);
mCheckAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCartAdapter.checkAll(isChecked);
}
});
mGoodsList = view.findViewById(R.id.list_cart);
mCartAdapter = new CartAdapter2();
mGoodsList.setAdapter(mCartAdapter);
mCartAdapter.setTotalPriceListener(this);//设置总价回调器
mGoodsList.setGroupIndicator(null);
mGoodsList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
return true;
}
});
cartPresenter.requestData();
return view;
}
@Override
public void success(List<Shop> data) {
mCartAdapter.addAll(data);
//遍历所有group,将所有项设置成默认展开
int groupCount=data.size();
for (int i = 0; i <groupCount ; i++) {
mGoodsList.expandGroup(i);
}
mCartAdapter.notifyDataSetChanged();
}
@Override
public void fail(Result result) {
Toast.makeText(getActivity(), result.getCode() + \" \" + result.getMsg(), Toast.LENGTH_LONG).show();
}
@Override
public void totalPrice(double totalPrice) {
mSumPrice.setText(String.valueOf(totalPrice));
}
}
增加减少的按钮
<? version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout ns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\">
<TextView
android:id=\"@+id/btn_add\"
android:layout_width=\"30dp\"
android:layout_height=\"30dp\"
android:background=\"@drawable/add\"
android:focusable=\"false\"
android:textSize=\"20sp\"
android:gravity=\"center\"
android:text=\"+\" />
<TextView
android:id=\"@+id/text_number\"
android:layout_width=\"60dp\"
android:layout_height=\"30dp\"
android:gravity=\"center\"
android:textSize=\"14sp\"
android:text=\"1000\" />
<TextView
android:id=\"@+id/btn_sub\"
android:layout_width=\"30dp\"
android:layout_height=\"30dp\"
android:textSize=\"20sp\"
android:focusable=\"false\"
android:gravity=\"center\"
android:background=\"@drawable/accept\"
android:text=\"-\" />
</LinearLayout>
增加减少的java
public class AddSubLayout extends LinearLayout implements View. Listener {
private TextView mAddBtn,mSubBtn;
private TextView mNumText;
private AddSubListener addSubListener;
public AddSubLayout(Context context) {
super(context);
initView();
}
public AddSubLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public AddSubLayout(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.set Listener(this);
mSubBtn.set Listener(this);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
int width = r-l;//getWidth();
int height = b-t;//getHeight();
}
@Override
public void (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 (addSubListener!=null){
addSubListener.addSub(number);
}
}
public void setCount(int count) {
mNumText.setText(count+\"\");
}
public void setAddSubListener(AddSubListener addSubListener) {
this.addSubListener = addSubListener;
}
public interface AddSubListener{
void addSub(int count);
}
}
二级列表第一级的布局
<? version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout ns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:orientation=\"horizontal\">
<CheckBox
android:id=\"@+id/checkBox\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_weight=\"1\"
android:focusable=\"false\"
android:text=\"CheckBox\" />
</LinearLayout>
二级列表孩子的布局
<? version=\"1.0\" encoding=\"utf-8\"?>
<RelativeLayout ns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:orientation=\"horizontal\">
<CheckBox
android:id=\"@+id/cart_goods_check\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_alignParentLeft=\"true\"
android:layout_centerVertical=\"true\"/>
<ImageView
android:id=\"@+id/image\"
android:layout_width=\"100dp\"
android:layout_height=\"wrap_content\"
android:adjustViewBounds=\"true\"
android:minHeight=\"50dp\"
android:layout_toRightOf=\"@+id/cart_goods_check\"
android:src=\"@mipmap/ic_launcher\"/>
<TextView
android:id=\"@+id/text\"
android:layout_toRightOf=\"@+id/image\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:layout_marginTop=\"10dp\"
android:text=\"aa\"
android:padding=\"10dp\"/>
<com.example.demo4.http.AddSubLayout
android:id=\"@+id/add_sub_layout\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_alignParentRight=\"true\"
android:layout_alignParentBottom=\"true\"
android:layout_marginRight=\"20dp\"
android:layout_marginBottom=\"20dp\">
</com.example.demo4.http.AddSubLayout>
<TextView
android:id=\"@+id/text_price\"
android:layout_toRightOf=\"@+id/image\"
android:layout_below=\"@+id/text\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:layout_marginTop=\"10dp\"
android:text=\"价格\"
android:padding=\"10dp\"/>
</RelativeLayout>
Adapter加载数据进入二级列表
public class CartAdapter2 extends ExpandableListAdapter {
private List<Shop> mList = new ArrayList<>();
private TotalPriceListener totalPriceListener;
public CartAdapter2(){
}
public void setTotalPriceListener(TotalPriceListener totalPriceListener) {
this.totalPriceListener = totalPriceListener;
}
@Override
public int getGroupCount() {
return mList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mList.get(groupPosition).getList().size();
}
@Override
public getGroup(int groupPosition) {
return mList.get(groupPosition);
}
@Override
public getChild(int groupPosition, int childPosition) {
return mList.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(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHodler holder;
if (convertView==null){
convertView=View.inflate(parent.getContext(), R.layout.item_father,null);
holder=new GroupHodler();
holder.checkBox=convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
}else {
holder= (GroupHodler) convertView.getTag();
}
final Shop shop=mList.get(groupPosition);
holder.checkBox.setText(shop.getSellerName());
holder.checkBox.setChecked(shop.isCheck());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
shop.setCheck(isChecked);
List<Goods> goodsList=mList.get(groupPosition).getList();
for (int i=0;i<goodsList.size();i++){
goodsList.get(i).setSellerid(isChecked?1:0);
}
notifyDataSetChanged();
calculatePrice();
}
});
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.item_son,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.addSub = convertView.findViewById(R.id.add_sub_layout);
holder.check = convertView.findViewById(R.id.cart_goods_check);
convertView.setTag(holder);
}else {
holder= (MyHolder) convertView.getTag();
}
final Goods goods = mList.get(groupPosition).getList().get(childPosition);
holder.text.setText(goods.get ());
holder.price.setText(\"单价:\"+goods.getPrice());//单价
holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
goods.setSelected(isChecked?1:0);
calculatePrice();//计算价格
}
});
if (goods.getSelected()==0){
holder.check.setChecked(false);
}else{
holder.check.setChecked(true);
}
String imageurl = \"https\" + goods.getImages().split(\"https\")[1];
Log.i(\"dt\", \"imageUrl: \" + imageurl);
imageurl = imageurl.substring(0, imageurl.lastIndexOf(\".jpg\") + \".jpg\".length());
if (parent.getContext()!=null){
Glide.with(DTApplication.getInstance()).load(imageurl).into(holder.image);//加载图片
}
holder.addSub.setCount(goods.getNum());//设置商品数量
holder.addSub.setAddSubListener(new AddSubLayout.AddSubListener() {
@Override
public void addSub(int count) {
goods.setNum(count);
calculatePrice();//计算价格
}
});
return convertView;
}
public void checkAll(boolean isCheck){
for (int i = 0; i < mList.size(); i++) {//循环的商家
Shop shop = mList.get(i);
shop.setCheck(isCheck);
for (int j = 0; j < shop.getList().size(); j++) {
Goods goods = shop.getList().get(j);
goods.setSelected(isCheck?1:0);
}
}
notifyDataSetChanged();
calculatePrice();
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}private void calculatePrice(){
double totalPrice=0;
for (int i = 0; i < mList.size(); i++) {//循环的商家
Shop shop = mList.get(i);
for (int j = 0; j < shop.getList().size(); j++) {
Goods goods = shop.getList().get(j);
if (goods.getSelected()==1) {//如果是选中状态
totalPrice = totalPrice + goods.getNum() * goods.getPrice();
}
}
}
if (totalPriceListener!=null)
totalPriceListener.totalPrice(totalPrice);
}
public void addAll(List<Shop> data) {
if (data != null)
mList.addAll(data);
}
class MyHolder {
CheckBox check;
TextView text;
TextView price;
ImageView image;
AddSubLayout addSub;
}
class GroupHodler {
CheckBox checkBox;
}
public interface TotalPriceListener{
void totalPrice(double totalPrice);
}
}
定义全局类
public class DTApplication extends Application {
private static DTApplication instance;
private SharedPreferences mSharedPreferences;
@Override
public void onCreate() {
super.onCreate();
instance = this;
mSharedPreferences = getSharedPreferences(\"application\",
Context.MODE_PRIVATE);
// JPushInterface.setDebugMode(true);
// JPushInterface.init(this); // 初始化 JPush
}
public static DTApplication getInstance() {
return instance;
}
public SharedPreferences getShare() {
return mSharedPreferences;
}
}
继续阅读与本文标签相同的文章
上一篇 :
Adobe 宣布推出全新 PDF 功能
-
Java并发系列(4)java关键字-synchronized
2026-05-18栏目: 教程
-
汇编(六)栈段、第一个汇编程序
2026-05-18栏目: 教程
-
目前Web前端就业前景如何 前端应用于哪些领域
2026-05-18栏目: 教程
-
kubernetes Service:让客户端发现pod并与之通信
2026-05-18栏目: 教程
-
新版本上线前期,产品经理要做那几点事?
2026-05-18栏目: 教程
