ExpandableListview的三级列表的数据加载及刷新

一、ExpandableListView初识

之前对于ExpandableListView的理解并不是很了解,由于最近的项目需要用到才开始认真的去查看下相关的知识,其实 ExpandableListView类似于listview,它同样也有自己的布局适配器,也同样有getView和getConunt方法,可以这么理解,ExpandableListView两个listview的结合,嵌套而已

二、ExpandableListView的初步使用
ExpandableListView有一级适配器和二级适配器,我们来看看一级适配器的代码:

public class ParentAdapter extends BaseExpandableListAdapter{

    private Realm realm;
    private Context mContext;
    private List<ParentEntity> parents;
    private List<ParentAdapterEntity> childAdapterLists;

    public ParentAdapter(Context mContext, List<ParentEntity> parents, Realm realm, List<ParentAdapterEntity> childAdapterLists) {
        this.mContext = mContext;
        this.parents = parents;
        this.realm = realm;
        this.childAdapterLists = childAdapterLists;
    }


    public void setData(List<ParentEntity> parents, List<ParentAdapterEntity> childAdapterLists) {
        this.parents = parents;
        this.childAdapterLists = childAdapterLists;
        notifyDataSetChanged();
    }

    public List<ParentEntity> getData(){
        return parents;
    }

    public List<ParentAdapterEntity> getAdapterList(){
        return childAdapterLists;
    }

    @Override
    public int getGroupCount() {
        return parents != null ? parents.size() : 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return parents.get(groupPosition).getChilds() != null ? parents
                .get(groupPosition).getChilds().size() : 0;
    }

    @Override
    public ParentEntity getGroup(int groupPosition) {
        return parents.get(groupPosition);
    }

    @Override
    public ChildEntity getChild(int groupPosition, int childPosition) {
        return parents.get(groupPosition).getChilds().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;
    }

    /**
     * 根布局
     * @param groupPosition
     * @param isExpanded
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.parent_group_item, null);
            holder = new GroupHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        if(!isExpanded){
            holder.parentGroup_iv.setBackgroundResource(R.mipmap.img_fangda);
        }else{
            holder.parentGroup_iv.setBackgroundResource(R.mipmap.img_suoxiao);
        }
       holder.update(parents.get(groupPosition),groupPosition);
        return convertView;
    }

    /**
     * 子列表的布局
     * @param groupPosition
     * @param childPosition
     * @param isLastChild
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, View convertView, ViewGroup parent) {
        final ExpandableListView eListView = getExpandableListView();
        final ArrayList<ChildEntity> childs = new ArrayList<ChildEntity>();
        final ChildEntity child = getChild(groupPosition, childPosition);
        childs.add(child);
        final ChildAdapter childAdapter = new ChildAdapter(this.mContext,
                childs);
        childAdapterLists.get(groupPosition).getChildAdapterEntitys().get(childPosition).setChildAdapter(childAdapter);
        //存储每一个adapter用来刷新
        eListView.setAdapter(childAdapter);
        eListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPos, long id) {

            }
        });
        /**
         * 设置子列表的条目点击事件
         * 点击子列表时,调用回调接口
         */
        eListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupIdex, int childIdex, long id) {

            }
        });
        childAdapterLists.get(groupPosition).getChildAdapterEntitys().get(childPosition).seteListView(eListView);
        return eListView;
    }

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

    /**
     * getchildview中动态创建子ExpandableListView
     * @return
     */
    public ExpandableListView getExpandableListView() {
        final CustomExpandableListView mExpandableListView = new CustomExpandableListView(mContext);
        mExpandableListView.setPadding(20,0,0,0);
        mExpandableListView.setGroupIndicator(null);// 取消展开折叠的指示图标
        return mExpandableListView;
    }

    /**
     * 根布局的viewholder
     */
    class GroupHolder {
        private TextView parentGroupTV;
        private ImageView parentGroup_iv;
        private ImageView iv_parent_run;

        public GroupHolder(View v) {
            parentGroupTV = (TextView)v.findViewById(R.id.parentGroupTV);
            parentGroup_iv = (ImageView)v.findViewById(R.id.parentGroup_iv);
            iv_parent_run = (ImageView)v.findViewById(R.id.iv_parent_run);
        }

        public void update(ParentEntity model, int groupPosition) {
            parentGroupTV.setText(model.getGroupName());
           parentGroupTV.setTextColor(Color.parseColor("#6F6F6F"));
            if (null != model.getChilds() && model.getChilds().size()>0){
                if (model.getChilds().get(0).getChildTaskBean().get(0).isParentRun()){
                    iv_parent_run.setVisibility(View.VISIBLE);
                }else{
                    iv_parent_run.setVisibility(View.INVISIBLE);
                }
            }else{
                RealmResults<ParentEntity> where = realm.where(ParentEntity.class).findAll();
                for (ParentEntity parentEntity:where) {
                    if (model.getGroupName().equals(parentEntity.getGroupName())){
                        if (parentEntity.getChilds().get(0).getChildTaskBean().get(0).isParentRun()){
                            iv_parent_run.setVisibility(View.VISIBLE);
                        }else{
                            iv_parent_run.setVisibility(View.INVISIBLE);
                        }
                        break;
                    }
                }
            }
        }
    }

核心代码在于getGroupView和getChildView,三级列表的话在getchildVie中在取创建新的ExpandableListView,然后给当前这个二级条目的ExpandableListView设置他的adpter,这样childadapter的使用就在这里了,我们看下面的核心代码:

/**
     * 第二层布局
     * @param groupPosition
     * @param isExpanded
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.child_group_item, null);
            holder = new GroupHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        if(!isExpanded){
            holder.childGroup_iv.setBackgroundResource(R.mipmap.img_fangda);
        }else{
            holder.childGroup_iv.setBackgroundResource(R.mipmap.img_suoxiao);
        }
        holder.update(mChilds.get(groupPosition));
        return convertView;
    }

    /**
     * 第三层布局
     * @param groupPosition
     * @param childPosition
     * @param isLastChild
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder = null;
        if (convertView == null) {
            convertView =LayoutInflater.from(mContext).inflate(R.layout.child_child_item, null);
            holder = new ChildHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        TaskBean taskBean = mChilds.get(groupPosition).getChildTaskBean().get(childPosition);
        holder.update(taskBean);
        return convertView;
    }
在三级列表的实现过程中,项目的需求是要求点击条目后,将当前条目的颜色改变,当时思考这个问题后,考虑定义一个专门用来存放adpter三级集合,分别对应数据集合的每一条数据,这样一来当我们想要刷新某一个条目的时候,我们通过记录当前点击的条目的position,(一级,二级,三级的position)adpter集合找出点击的适配器后,改变对象的颜色,然后调用刷新,还要将上一个点击的颜色修改回来,这里同样也要记录上一个点击的条目,也是调用adpter刷新;还有一个重点就是ExpandableListView调用setOnGroupClickListener的时候,方法会有一个返回boolean值,如果返回true,那么条目将不可展开,这个我们要是想动态的不想展开,可以自己在代码中动态输入后改变;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值