二级列表和全选

本文介绍了如何使用HTML和CSS构建具备二级结构的列表,并实现全选功能。通过示例代码,读者将了解到如何组织HTML结构以展示多级列表,以及如何利用CSS样式控制列表的呈现效果。同时,文章还将讲解如何添加JavaScript来实现一键全选和取消全选的交互体验。
摘要由CSDN通过智能技术生成

compile 'org.greenrobot:eventbus:3.0.0'

主布局代码

package com.example.cll.erjigouwuche;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ExpandableListView mElv;
    /**
     * 全选
     */
    private CheckBox mCbAll;
    /**
     * 合计:
     */
    private TextView mTvTotal;
    private List<Groupbean> groupList = new ArrayList<>();
    private List<List<Childbean>> childList = new ArrayList<>();
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        initView();
        //给设置ExpandableListView设置数据
        //模拟数据
        initData();
        adapter = new MyAdapter(this, groupList, childList);
        mElv.setGroupIndicator(null);
        mElv.setAdapter(adapter);
        //全部展开
        for (int i = 0; i < groupList.size(); i++) {
            mElv.expandGroup(i);
        }
        //给全选设置点击事件
        mCbAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.allChecked(mCbAll.isChecked());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void messageCountEvent(MessageCounEvent msg) {
        mTvTotal.setText("总计:" + msg.getCount() + "个");
    }

    @Subscribe
    public void messageEvent(MessageEvent msg) {
        mCbAll.setChecked(msg.isFlag());
    }

    private void initData() {
        for (int i = 0; i < 3; i++) {
            Groupbean groupBean = new Groupbean(false,"商家" + i);
            groupList.add(groupBean);
            List<Childbean> list = new ArrayList<>();
            for (int j = 0; j < 2; j++) {
                Childbean childBean = new Childbean(false,"商品" + j );
                list.add(childBean);
            }
            childList.add(list);
        }
    }

    private void initView() {
        mElv = (ExpandableListView) findViewById(R.id.elv);
        mCbAll = (CheckBox) findViewById(R.id.cbAll);
        mTvTotal = (TextView) findViewById(R.id.tvTotal);
    }

}
主布局
<?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"
    android:orientation="vertical"
    tools:context="com.example.cll.erjigouwuche.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#ff3660"
        android:gravity="center"
        android:text="购物车"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <ExpandableListView
        android:id="@+id/elv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#33000000">

        <CheckBox
            android:id="@+id/cbAll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="全选" />

        <TextView
            
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值