二级列表简单实现二级列表

   二级列表控件:

   

ExpandableListView
  一般购物车都是有个表头 有个表位所以要用一个
ScrollView 控件包裹着,一般会出现滑动冲突事件所以需要自定义一个二级列表
public class MyExpandableListView extends ExpandableListView {
    public MyExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //解决滑动事件冲突
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, height);
    }
}
注意这里需要在bean类里写一个自定义的属性为了记录一级列表的checkbox框的状态:
public boolean isGroupChecked() {
    return isGroupChecked;
}


public void setGroupChecked(boolean groupChecked) {
    isGroupChecked = groupChecked;
}

private boolean isGroupChecked;
//首次进入app时isGeoupchecked为false



直接看适配器的逻辑代码,重点地方都有注释的:
public class Myadapter extends BaseExpandableListAdapter {
    private Handler handler;
    private Context context;
    private List<Bean.DataBean> group_list;
    private List<List<Bean.DataBean.ListBean>>child_list;

    public Myadapter(Handler handler, Context context, List<Bean.DataBean> group_list, List<List<Bean.DataBean.ListBean>> child_list) {
        this.handler = handler;
        this.context = context;
        this.group_list = group_list;
        this.child_list = child_list;
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child_list.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 true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder;
        if(convertView==null){
            convertView = View.inflate(context, R.layout.group_item, null);
            holder = new GroupHolder();
            holder.group_checked=convertView.findViewById(R.id.group_checked);
            holder.group_text=convertView.findViewById(R.id.group_text);
            convertView.setTag(holder);
        }else {
            holder= (GroupHolder) convertView.getTag();
        }
        final Bean.DataBean dataBean = group_list.get(groupPosition);
        holder.group_checked.setChecked(dataBean.isGroupChecked());
        holder.group_text.setText(dataBean.getSellerName());
        holder.group_checked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //改变当前以及列表的状态,不与初始化的相等
                dataBean.setGroupChecked(!dataBean.isGroupChecked());
                //根据当前一级状态改变二级列表的值
                changeChildState(groupPosition,dataBean.isGroupChecked());
                //通过判断一级的是否选中,来设置是否为全部选中
                changeAllState(isAllGroupChecked());
                //发送价格数量
                sendPriceAndCount();
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
         ChildHolder holder;
        if(view==null){
            view = View.inflate(context, R.layout.child_item, null);
            holder=new ChildHolder();

            holder.text_add = view.findViewById(R.id.text_add);
            holder.text_num = view.findViewById(R.id.text_num);
            holder.text_jian = view.findViewById(R.id.text_jian);
            holder.text_title = view.findViewById(R.id.text_title);
            holder.text_price = view.findViewById(R.id.text_price);
            holder.image_good = view.findViewById(R.id.image_good);
            holder.check_child = view.findViewById(R.id.check_child);

            view.setTag(holder);
        }else {
            holder = (ChildHolder) view.getTag();
        }
        final Bean.DataBean.ListBean listBean = child_list.get(groupPosition).get(childPosition);
        holder.text_num.setText(listBean.getNum()+"");//......注意
        holder.text_price.setText("¥"+listBean.getPrice());
        holder.text_title.setText(listBean.getTitle());
        ImageLoader.getInstance().displayImage(listBean.getImages().split("!")[0],holder.image_good);
        holder.check_child.setChecked(listBean.getSelected()==0? false:true);
        holder.check_child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listBean.setSelected(listBean.getSelected() ==0? 1:0);
                sendPriceAndCount();
                if(listBean.getSelected()==1){
                   if(isAllChildSelected(groupPosition)){
                       //如果全部选中改变当前状态
                       changeGroupState(groupPosition,true);
                       //.确定是否改变全选
             
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值