Android 可以全选 单选 展开收起的ExpandListView列表

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Mr___Xu/article/details/93490500

如图:

1.activity

public class HousingBillsActivity extends BaseActivity {

    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.tvTitleName)
    TextView tvTitleName;
    @BindView(R.id.tvAddress)
    TextView tvAddress;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvClickAll)
    TextView tvClickAll;
    @BindView(R.id.expanfListView)
    ExpandableListView expanfListView;
    @BindView(R.id.tvJine)
    TextView tvJine;
    @BindView(R.id.tvjiaofei)
    TextView tvjiaofei;
    private Context context = this;
    private BillsExpandListView billsExpandListView;
    private List<String> group;
    private List<List<String>> child;
    private List<CheckListViewBean> beans;
    private boolean choose = true;
    private List<String> test = new ArrayList<>();
    private PopupWindow mPopWindow;
    private String pay = "";

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_housing_bills);
        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        tvTitleName.setText("房屋账单");
        group = new ArrayList<String>();
        child = new ArrayList<List<String>>();
        beans = new ArrayList<>();
        addInfo("高层物业费", new String[]{"账单周期", "账单日"}, false);
        addInfo("电梯费", new String[]{"电梯费", "电费"}, false);
        billsExpandListView = new BillsExpandListView(context, group, child, beans);
        expanfListView.setAdapter(billsExpandListView);
        test.add("100");
        test.add("200");
    }

    public void addInfo(String g, String[] c, boolean b) {
        group.add(g);
        beans.add(new CheckListViewBean(b));
        List<String> item = new ArrayList<>();
        for (int i = 0; i < c.length; i++) {
            item.add(c[i]);
        }
        child.add(item);
    }

    @OnClick({R.id.rlBack, R.id.tvClickAll, R.id.tvjiaofei})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rlBack:
                finish();
                break;
            case R.id.tvClickAll:
                if (choose) {
                    for (int i = 0; i < beans.size(); i++) {
                        beans.get(i).checked = true;
                    }
                    Drawable drawableLeft = getResources().getDrawable(
                            R.drawable.bills_add_z);
                    tvClickAll.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
                            null, null, null);
                    billsExpandListView.notifyDataSetChanged();
                    int sums = 0;
                    for (int i = 0; i < test.size(); i++) {
                        int j = Integer.parseInt(test.get(i));
                        sums += j;
                    }
                    tvJine.setText(sums + "");
                    choose = false;
                } else {
                    for (int i = 0; i < beans.size(); i++) {
                        beans.get(i).checked = false;
                    }
                    Drawable drawableLeft = getResources().getDrawable(
                            R.drawable.bills_add);
                    tvClickAll.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
                            null, null, null);
                    billsExpandListView.notifyDataSetChanged();
                    tvJine.setText("0");
                    choose = true;
                }
                break;
            case R.id.tvjiaofei:
           
                break;
        }
    }

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

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent event) {
        if (event.getType().equals("账单")) {
            btnOperateList();
        }
    }

    public void btnOperateList() {
        List<String> ids = new ArrayList<>();
        for (int i = 0; i < beans.size(); i++) {
            if (beans.get(i).checked) {
                ids.add(test.get(i));
            }
            if (!beans.get(i).checked) {//设置如果有一个项没选就取消全选
                Drawable drawableLeft = getResources().getDrawable(
                        R.drawable.bills_add);
                tvClickAll.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
                        null, null, null);
            }
        }
        if (ids.size() == group.size()) {//把选中的添加到一个集合 然后用选中集合的size和总数据的size对比  相同的话就证明是全选了
            Drawable drawableLeft = getResources().getDrawable(
                    R.drawable.bills_add_z);
            tvClickAll.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
                    null, null, null);
        }
        int sums = 0;//计算list中数据求和
        for (int i = 0; i < ids.size(); i++) {
            int j = Integer.parseInt(ids.get(i));
            sums += j;
        }
        tvJine.setText(sums + "");
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (mPopWindow != null && mPopWindow.isShowing()) {
            return false;
        }
        return super.dispatchTouchEvent(event);
    }
}

2.BillsExpandListView

private Context context;
List<String> group;
List<List<String>> child;
List<CheckListViewBean> beans;

public BillsExpandListView(Context context, List<String> group, List<List<String>> child, List<CheckListViewBean> beans) {
    this.context = context;
    this.group = group;
    this.child = child;
    this.beans = beans;
}

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

@Override
public int getChildrenCount(int groupPosition) {
    return child.size();

}

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

}

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

@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    final GroupViewHolder groupViewHolder;
    if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.partent_item, parent, false);
        groupViewHolder = new GroupViewHolder();
        groupViewHolder.tvName = convertView.findViewById(R.id.tvName);
        groupViewHolder.tvMoney = convertView.findViewById(R.id.tvMoney);
        groupViewHolder.ivJiantou = convertView.findViewById(R.id.ivJiantou);
        groupViewHolder.checkbox = convertView.findViewById(R.id.checkbox);
        convertView.setTag(groupViewHolder);
    } else {
        groupViewHolder = (GroupViewHolder) convertView.getTag();
    }
    groupViewHolder.tvName.setText(group.get(groupPosition));
    // 根据当前父条目的展开状态来设置不同的图片
    if (isExpanded) {
        // 条目未展开,设置向上的箭头
        groupViewHolder.ivJiantou.setImageDrawable(context.getResources()
                .getDrawable(R.drawable.bills_up));
    } else {
        // 条目展开,设置向下的箭头
        groupViewHolder.ivJiantou.setImageDrawable(context.getResources()
                .getDrawable(R.drawable.bills_dwon));
    }
    groupViewHolder.checkbox.setChecked(beans.get(groupPosition).checked);
    groupViewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (beans.get(groupPosition).checked) {
                beans.get(groupPosition).checked = false;
            } else {
                beans.get(groupPosition).checked = true;
            }
            EventBus.getDefault().post(new MessageEvent("账单", ""));
        }
    });
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ChildViewHolder childViewHolder;
    if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_item, parent, false);
        childViewHolder = new ChildViewHolder();
        childViewHolder.tvName = convertView.findViewById(R.id.tvName);
        childViewHolder.tvTime = convertView.findViewById(R.id.tvTime);
        convertView.setTag(childViewHolder);
    } else {
        childViewHolder = (ChildViewHolder) convertView.getTag();
    }
    childViewHolder.tvName.setText(child.get(groupPosition).get(childPosition));
    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

static class GroupViewHolder {
    TextView tvName, tvMoney;
    ImageView ivJiantou;
    CheckBox checkbox;
}

static class ChildViewHolder {
    TextView tvName, tvTime;

}

3.CheckListViewBean

public class CheckListViewBean {
    public boolean checked;

    public CheckListViewBean(boolean check) {
        checked = check;
    }
}

4.partent_item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/RGB_FFFFFF"
    android:orientation="vertical">


    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        style="@style/CustomCheckboxTheme"
        android:focusable="false" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/checkbox"
        android:text="高层物业"
        android:textColor="@color/RGB_000000"
        android:textSize="16sp" />

    <ImageView
        android:id="@+id/ivJiantou"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="16dp"
        android:src="@drawable/bills_dwon" />

    <TextView
        android:id="@+id/tvMoney"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toLeftOf="@+id/ivJiantou"
        android:text="¥2008.12元"
        android:textColor="@color/RGB_000000"
        android:textSize="16sp" />
</RelativeLayout>

5.child_item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/RGB_FFFFFF"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="53dp"
        android:text="账单周期"
        android:textColor="@color/RGB_000000"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tvTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="18dp"
        android:text="2018-08-24"
        android:textColor="@color/RGB_000000"
        android:textSize="16sp" />
</RelativeLayout>

6.activity_housing_bills

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/RGB_EFEFF4"
    android:orientation="vertical">

    <include
        android:id="@+id/il"
        layout="@layout/layout_titlebar_x" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/il"
        android:background="@color/RGB_EFEFF4"
        android:orientation="vertical">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@color/RGB_FFFFFF">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="18dp"
                android:layout_marginBottom="18dp"
                android:text="缴费地址"
                android:textColor="@color/RGB_000000"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/tvAddress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="16dp"
                android:text="隆德华府11栋2单元209"
                android:textColor="@color/RGB_000000"
                android:textSize="14sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/RGB_FFFFFF">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="18dp"
                android:layout_marginBottom="18dp"
                android:text="业主姓名"
                android:textColor="@color/RGB_000000"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/tvName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="16dp"
                android:text="*志强"
                android:textColor="@color/RGB_000000"
                android:textSize="14sp" />
        </RelativeLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:background="@color/RGB_E5E5EA" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/RGB_FFFFFF">

            <TextView
                android:id="@+id/tvClickAll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="18dp"
                android:layout_marginBottom="18dp"
                android:drawableLeft="@drawable/bills_add"
                android:drawablePadding="8dp"
                android:text="全选"
                android:textColor="@color/RGB_000000"
                android:textSize="16sp" />
        </RelativeLayout>

        <ExpandableListView
            android:id="@+id/expanfListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:childDivider="@color/RGB_FFFFFF"
            android:divider="@null"
            android:groupIndicator="@null" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:layout_alignParentBottom="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@+id/tvjiaofei"
            android:background="@color/RGB_FFFFFF"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="实付款"
                android:textColor="@color/RGB_000000"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/tvJine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textColor="#F90F0F"
                android:textSize="16sp" />
        </LinearLayout>

        <TextView
            android:id="@+id/tvjiaofei"
            android:layout_width="174dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:background="@color/RGB_FFD942"
            android:gravity="center"
            android:text="立即缴费"
            android:textColor="@color/RGB_000000"
            android:textSize="18sp" />

    </RelativeLayout>

</RelativeLayout>

7.MessageEvent

package com.tsq.junbanpt.aiui.bean;

public class MessageEvent {
    private String type;
    private String contect;

    public MessageEvent(String type, String contect) {
        this.type = type;
        this.contect = contect;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getContect() {
        return contect;
    }

    public void setContect(String contect) {
        this.contect = contect;
    }
}

8.用到eventbus

implementation 'org.greenrobot:eventbus:3.1.1'

猜你喜欢

转载自blog.csdn.net/Mr___Xu/article/details/93490500