二级列表的简单用法

布局代码:

<ExpandableListView
   android:visibility="gone"
    android:id="@+id/pt_lv"
    android:descendantFocusability = "blocksDescendants"
    android:divider="@null"
    android:scrollbars="none"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</ExpandableListView>

主页面代码:


  pt_lv = (ExpandableListView) view.findViewById(R.id.pt_lv);
carPersonUtils.setCarPersonInterface(new CarPersonInterface() {

                //请求数据成功
                @Override
                public void CarSuccess(final CarBean bean) {

                    List<CarBean.DataBean> data = bean.getData();
                    list_shop = new ArrayList<Car_shop_Bean>();
                    list_shop_chiled = new ArrayList<List<Car_child_shop_Bean>>();
                    //添加进二级列表的 
for (int i = 0; i < data.size(); i++) {
    String sellerName = data.get(i).getSellerName();
    list_shop.add(new Car_shop_Bean(sellerName, false));

    List<CarBean.DataBean.ListBean> list = data.get(i).getList();

    ArrayList<Car_child_shop_Bean> car_shop_been = new ArrayList<>();

             for (CarBean.DataBean.ListBean childList : list) {
                  int num = childList.getNum();
                  float price = childList.getPrice();
                  String images = childList.getImages();
                  String title = childList.getTitle();
                  String[] split = images.toString().split("\\|");
                 car_shop_been.add(new Car_child_shop_Bean(title, num, price, split[0], false));
                   }
                     list_shop_chiled.add(car_shop_been);
                 }
//让他默认收回(并展示)
                    int count = pt_lv.getCount();
                    for (int i = 0; i < count; i++) {
                        pt_lv.expandGroup(i);
                    }
                    pt_lv.setGroupIndicator(null);
                }

  //不可视状态将刷新开启
    @Override
    public void onPause() {
        super.onPause();
        isFlag = true;
    }
    //得到焦点时的方法
    @Override
    public void onResume() {
        super.onResume();
        if(isFlag){
            initCarriage();
        }
    }

适配器

public class CarShopAdapter extends BaseExpandableListAdapter{
    private Context context;
    private ArrayList<Car_shop_Bean> list_shop;
    private ArrayList<List<Car_child_shop_Bean>> list_shop_child;
    private CarShopCheckedInterface carShopCheckedInterface;
    private ShopNumInterface shopNumInterface;
    private NumberAndPrice numberAndPrice;
    private CheckBox rb_shop;
    private TextView tv_shopName;
    private CheckBox rb_commodity;
    private ImageView iv_commodityImg;
    private TextView tv_commodityName;
    private TextView tv_commodityPrice;
    private Button btn_delete;
    private Button btn_add;
    private EditText et_num;
    private int shopNum = 0;
    private int allNum = 0;
    private int allPrice = 0;
    MainActivity mainActivity = (MainActivity) context;
    public CarShopAdapter(Context context, ArrayList<Car_shop_Bean> list_shop, ArrayList<List<Car_child_shop_Bean>> list_shop_child) {
        this.context = context;
        this.list_shop = list_shop;
        this.list_shop_child = list_shop_child;
    }
    public void setCarShop(CarShopCheckedInterface carShopCheckedInterface){
        this.carShopCheckedInterface = carShopCheckedInterface;
    }
    public void setShopList(ShopNumInterface shopNumInterface){
        this.shopNumInterface = shopNumInterface;
    }
    public void setNumberAndPrice(NumberAndPrice numberAndPrice){
        this.numberAndPrice = numberAndPrice;
    }
    @Override
    public int getGroupCount() {
        return list_shop.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return list_shop_child.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return list_shop.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return list_shop_child.get(i).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        final Car_shop_Bean car_shop_bean1 = list_shop.get(i);
        final List<Car_child_shop_Bean> car_child_shop_been = list_shop_child.get(i);
            view = View.inflate(context, R.layout.car_shop_one_fragment, null);
            rb_shop = (CheckBox)view.findViewById(R.id.rb_shop);
            tv_shopName = (TextView)view.findViewById(R.id.tv_shopName);
            rb_shop.setChecked(car_shop_bean1.getFlag());
            tv_shopName.setText(car_shop_bean1.getName());
            rb_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("---------点击:",rb_shop.isChecked()+"");
                    list_shop.get(i).setFlag(rb_shop.isChecked());
                    boolean checked = rb_shop.isChecked();
                    car_shop_bean1.setFlag(checked);
                    boolean a = true;
                        for (int j = 0; j < list_shop.size(); j++) {
                            Boolean flag = list_shop.get(j).getFlag();
                            if(flag==false){
                                a = false;
                                carShopCheckedInterface.AllChecked(a);
                                break;
                            }else{
                                a = true;
                                carShopCheckedInterface.AllChecked(a);
                            }
                        }
                        for (int j = 0; j < car_child_shop_been.size(); j++) {
                            car_child_shop_been.get(j).setChildCheck(rb_shop.isChecked());
                        }
                        notifyDataSetChanged();
                        for (Car_child_shop_Bean list_child:car_child_shop_been){
                            numberAndPrice.AllNumAndPriceClick(list_shop_child,i);
                        }
                    }
                });
        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        final Car_shop_Bean car_shop_bean = list_shop.get(i);
        final Car_child_shop_Bean car_child_shop_bean = list_shop_child.get(i).get(i1);
        view = View.inflate(context, R.layout.car_shop_fragment, null);
        rb_commodity = (CheckBox)view.findViewById(R.id.rb_commodity);
        iv_commodityImg = (ImageView)view.findViewById(R.id.iv_commodityImg);
        tv_commodityName = (TextView)view.findViewById(R.id.tv_commodityName);
        tv_commodityPrice = (TextView)view.findViewById(R.id.tv_commodityPrice);
        btn_delete = (Button)view.findViewById(R.id.btn_delete);
        btn_add = (Button)view.findViewById(R.id.btn_add);
        et_num = (EditText)view.findViewById(R.id.et_num);
        et_num.setText(car_child_shop_bean.getNum()+"");
        rb_commodity.setChecked(car_child_shop_bean.isChildCheck());
        tv_commodityName.setText(car_child_shop_bean.getTitle());
        Glide.with(context).load(car_child_shop_bean.getImages()).into(iv_commodityImg);
        tv_commodityPrice.setText("¥:"+car_child_shop_bean.getPrice());
        btn_delete.setText("—");
        btn_add.setText("+");
        notifyDataSetChanged();


        rb_commodity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,rb_commodity.isChecked()+"",Toast.LENGTH_SHORT).show();
                car_child_shop_bean.setChildCheck(rb_commodity.isChecked());
                boolean flag = true;



               for(List<Car_child_shop_Bean> list_chiled:list_shop_child){
                   for (int j = 0; j < list_chiled.size(); j++) {
                       boolean childCheck = list_chiled.get(j).isChildCheck();

                       if(!childCheck){
                           carShopCheckedInterface.AllChecked(false);
                           list_shop.get(j).setFlag(false);
                           flag = false;
                           break;
                       }else{
                           carShopCheckedInterface.AllChecked(true);
                           list_shop.get(j).setFlag(true);
                           flag = true;
                       }
                   }
                   if(!flag){
                       break;
                   }
               }
                numberAndPrice.AllNumAndPriceClick(list_shop_child,i1);
                notifyDataSetChanged();
            }
        });

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean childCheck = car_child_shop_bean.isChildCheck();
                int num1 = car_child_shop_bean.getNum();
                num1++;
                car_child_shop_bean.setNum(num1);
                numberAndPrice.AllNumAndPriceClick(list_shop_child,i1);
                notifyDataSetChanged();
            }
        });
        final boolean childCheck = car_child_shop_bean.isChildCheck();
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num1 = car_child_shop_bean.getNum();
                num1--;
                if(num1<1){
                    car_child_shop_bean.setNum(1);
                    Toast.makeText(context,"不能再少了···",Toast.LENGTH_SHORT).show();
                }else{
                    car_child_shop_bean.setNum(num1);
                }
                numberAndPrice.AllNumAndPriceClick(list_shop_child,i1);
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}

猜你喜欢

转载自blog.csdn.net/dealpoor/article/details/78613192