Android ExpandableListView的使用
Android ExpandableListView的使用
汇聚成河 发表于4个月前
Android ExpandableListView的使用
  • 发表于 4个月前
  • 阅读 10
  • 收藏 0
  • 点赞 0
  • 评论 0

(文中说的一级标签二级标签也就是所谓的父级和子级) 在Android开发中会遇到点击时弹出二级标签的效果,可以使用Android自带的ExpandableListView,其用法跟Listview类似,不同点在于其使用的Adapter中需要重写的内容比较多。其中Adapter分为BaseExpandableListAdapter和ExpandableListAdapter,BaseExpandableListAdapter扩展了ExpandableListAdapter,使用起来也更加方便,可以刷新adapter,判断一级标签总数大小是否为0. 其中继承自ExpandableListAdapter的写法如下:

package com.okkuaixiu.combo.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;

public class MyExpandListAdapter implements ExpandableListAdapter{

    private ArrayList<AddQuotationBean.ASQuotations> list_quotation;//一级标签的数据源
    private ArrayList<ArrayList<AddQuotationBean.QDetails>> listsQDetail;//二级标签
    private Context context;

    public MyExpandListAdapter(ArrayList<AddQuotationBean.ASQuotations> list_quotation, Context context) {
        this.list_quotation = list_quotation;
        this.context = context;
        //为二级标签添加数据
        if (list_quotation != null && list_quotation.size() > 0) {
            listsQDetail = new ArrayList<>();
            for (AddQuotationBean.ASQuotations asquotations:
                    list_quotation) {
                listsQDetail.add(asquotations.QDetails);
            }
        }
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    //获取一级标签总数
    @Override
    public int getGroupCount() {
        return list_quotation.size();
    }

    //获取二级标签总数
    @Override
    public int getChildrenCount(int groupPosition) {
        return listsQDetail.size();
    }

    //获取一级标签内容
    @Override
    public Object getGroup(int groupPosition) {
        return list_quotation.get(groupPosition);
    }

    //获取二级标签内容
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return listsQDetail.get(groupPosition).get(childPosition);
    }

    //获取一级标签的ID
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //获取二级标签的ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //指定位置相应的组视图
    @Override
    public boolean hasStableIds() {
        return true;
    }

    //对一级标签进行设置
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        // 为视图对象指定布局
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_price_menu_detail, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_space_name = (TextView) convertView.findViewById(R.id.price_detail_space_name);
            viewHolder.tv_diff_sum = (TextView) convertView.findViewById(R.id.price_detail_diff);
            viewHolder.item_price_linelayout = (View) convertView.findViewById(R.id.item_price_linelayout);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        AddQuotationBean.ASQuotations asQuotations = list_quotation.get(groupPosition);
        viewHolder.tv_space_name.setText(asQuotations.SpaceName);
        viewHolder.tv_diff_sum.setText("¥" + Util.getNonScientificCount(asQuotations.SpaceTotalMoney + "", true));
        //二级标签是否展开
        if (isExpanded) {
            viewHolder.item_price_linelayout.setVisibility(View.GONE);
        } else {
            viewHolder.item_price_linelayout.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    //一级标签对应的内部类
    private class ViewHolder{
        private TextView tv_space_name;// 空间名
        private TextView tv_diff_sum;// 差价小计
        private View item_price_linelayout;//底部分割布局
        private LinearLayout ll_info;// 材料信息
        private RelativeLayout price_detail_layout;//空间名和差价小计整体的布局
    }
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        try {
            ChildViewHolder childViewHolder = null;
            if (convertView == null) {
                childViewHolder = new ChildViewHolder();
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.item_price_menu_detail_info, null);
                childViewHolder.detail_construct_name = (TextView) convertView.findViewById(R.id.info_construct_name);//施工项目
                childViewHolder.detail_material_name = (TextView) convertView.findViewById(R.id.info_material_name);//材料名称
                childViewHolder.detail_material_brand = (TextView) convertView.findViewById(R.id.info_brand);//材料品牌
                childViewHolder.detail_material_model = (TextView) convertView.findViewById(R.id.info_model);//材料型号
                childViewHolder.detail_diff = (TextView) convertView.findViewById(R.id.info_price_diff);//单位差价
                childViewHolder.detail_count = (TextView) convertView.findViewById(R.id.info_count);//数量
                childViewHolder.item_price_info_linelayout = (View) convertView.findViewById(R.id.item_price_info_linelayout);// 底部间隔布局
                convertView.setTag(childViewHolder);
            } else {
                childViewHolder = (ChildViewHolder) convertView.getTag();
            }
            //当二级标签的内容展开时再显示间隔布局
            if (isLastChild) {
                childViewHolder.item_price_info_linelayout.setVisibility(View.VISIBLE);
            } else {
                childViewHolder.item_price_info_linelayout.setVisibility(View.GONE);
            }
            AddQuotationBean.QDetails qDetails = listsQDetail.get(groupPosition).get(childPosition);
            childViewHolder.detail_construct_name.setText(qDetails.ConstructionName);
            childViewHolder.detail_material_name.setText(qDetails.MateralName);
            childViewHolder.detail_material_brand.setText(qDetails.BrandName);
            childViewHolder.detail_material_model.setText(qDetails.MateralCode);
            childViewHolder.detail_diff.setText("¥" + Util.getNonScientificCount(qDetails.Difference + "", true));
            childViewHolder.detail_count.setText(qDetails.DMNumber + (!Util.isTextNull(qDetails.Unit) ? qDetails.Unit : ""));
        } catch (Exception e) {
            LogUtil.e(getClass(), "getChildView", e);
        }
        return convertView;
    }
    //二级标签对应的内部类
    private class ChildViewHolder{
        private TextView detail_construct_name;// 施工项目
        private TextView detail_material_name;//材料名称
        private TextView detail_material_brand;//材料品牌
        private TextView detail_material_model;//材料型号
        private TextView detail_diff;//单位差价
        private TextView detail_count;//数量
        private View item_price_info_linelayout;// 底部间隔布局
    }

    //子列表项是否能否触发事件,返回true则为可以响应点击 
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }
}

代码注释如上。 针对ExpandableListView的属性有一些要说明的地方有:android:groupIndicator="@null"表示去掉默认的图标箭头。如果设置了android:divider,那么其一级标签和二级标签中都会出现

共有 人打赏支持
粉丝 2
博文 25
码字总数 15859
×
汇聚成河
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: