expandableListView点击高亮

adapter=new My_Control_Adapter(getActivity(),lists);
expandableListView = (ExpandableListView) v.findViewById(R.id.control_expandable);
expandableListView.setAdapter(adapter);
expandableListView.setGroupIndicator(null); // 去掉默认带的箭头
expandableListView.setSelection(0);// 设置默认选中项
adapter.setselect(0);

for (int i = 0; i < adapter.getGroupCount(); i++) {
    expandableListView.collapseGroup(i);
}

expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
    @Override
    public void onGroupExpand(int groupPosition) {
        for (int i = 0, count = expandableListView
                .getExpandableListAdapter().getGroupCount(); i < count; i++) {
            if (groupPosition != i) {// 关闭其他分组
                expandableListView.collapseGroup(i);
                for(int j=0;j<adapter.getChildrenCount(i);j++)
                expandableListView.setItemChecked(j, false);
            }
        }
    }
});
expandableListView.expandGroup(0);

expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {

        adapter.setSelectedItem(groupPosition,childPosition);
        adapter.notifyDataSetChanged();
        return true;
    }
});


private List<GroupEntity> initList() {

    List<GroupEntity> groupList;
    //测试数据
    String[] groupArray = new String[] { "大厅","主卧", "次卧","厨房"};
    String[][] childTimeArray = new String[][] {
            { "测试数据1",  "测试数据2", "测试数据3" },
            {  "测试数据4"}, { "测试数据5", "测试数据6" } ,{ "测试数据7", "测试数据8" }};
    groupList = new ArrayList<GroupEntity>();
    for (int i = 0; i < groupArray.length; i++){
        GroupEntity groupEntity = new GroupEntity(groupArray[i]);
        List<ChildEntity> childList = new ArrayList<ChildEntity>();
        for (int j = 0; j < childTimeArray[i].length; j++) {
            ChildEntity childStatusEntity = new ChildEntity(childTimeArray[i][j]);
            childList.add(childStatusEntity);
        }
        groupEntity.setChildEntities(childList);
        groupList.add(groupEntity);
    }
    return groupList;
}



public class GroupEntity {

    private String groupName;
    private List<ChildEntity> childEntities;

    public GroupEntity(String groupName) {
        this.groupName = groupName;
    }

    public List<ChildEntity> getChildEntities() {
        return childEntities;
    }

    public void setChildEntities(List<ChildEntity> childEntities) {
        this.childEntities = childEntities;
    }

    public String getGroupName() {
        return groupName;
    }
}



public class ChildEntity {
    private String childTitle;


    public ChildEntity(String childTitle) {
        this.childTitle = childTitle;
    }

    public String getChildTitle() {
        return childTitle;
    }

}



public class My_Control_Adapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater = null;
    private List<GroupEntity> groupList;
    private Context context;
    private int select=-1;
    ControlFragment controlFragment = new ControlFragment();
    private int groupPositions;
    SparseBooleanArray selected;
    int old = -1;
    int parentPosition = -1;
    /**
     * 构造方法
     *
     * @param context
     * @param group_list
     */
    public My_Control_Adapter(Context context,List<GroupEntity> group_list) {
        this.context=context;
        this.groupList = group_list;
        inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        selected = new SparseBooleanArray();
    }

    /**
     * 返回一级Item总数
     */
    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return groupList.size();
    }

    /**
     * 返回二级Item总数
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        if (groupList.get(groupPosition).getChildEntities() == null) {
            return 0;
        } else {
            return groupList.get(groupPosition).getChildEntities().size();
        }
    }

    /**
     * 获取一级Item内容
     */
    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupList.get(groupPosition);
    }

    /**
     * 获取二级Item内容
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupList.get(groupPosition).getChildEntities().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {

        GroupViewHolder holder = new GroupViewHolder();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.group_status_item1, null);
        }
        holder.groupName = (TextView) convertView .findViewById(R.id.one_status_name);
        holder.groupimg = (ImageView) convertView.findViewById(R.id.img_group);
        holder.groupName.setText(groupList.get(groupPosition).getGroupName());

        if(isExpanded){
            holder.groupimg.setBackgroundResource(R.mipmap.device_normal);
//            if (groupPosition == groupPositions) {
//                controlFragment.setItemCheck();
//                notifyDataSetChanged();
//            }
        }else{
            holder.groupimg.setBackgroundResource(R.mipmap.device_normal);
        }

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder = null;
        final ChildEntity entity = (ChildEntity) getChild(groupPosition,
                childPosition);
        if (convertView != null) {
            viewHolder = (ChildViewHolder) convertView.getTag();
        } else {
            viewHolder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.child_status_item1, null);
            viewHolder.childTitle = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.surface = (LinearLayout) convertView.findViewById(R.id.item_surface);
            convertView.setTag(viewHolder);
        }
        if (entity!=null) {
            viewHolder.childTitle.setText(entity.getChildTitle());
        }

        if (selected.get(childPosition)&&this.parentPosition==groupPosition) {
            viewHolder.surface.setBackgroundColor(Color.BLUE);
        } else {
            // convertView.setBackgroundResource(R.color.white);
            viewHolder.surface.setBackgroundColor(Color.WHITE);
        }

        return convertView;
    }


    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private class GroupViewHolder {
        TextView groupName;
        ImageView groupimg;
    }
    private class ChildViewHolder {
        public TextView childTitle;
        public LinearLayout surface;
    }
    private void toast(String msg) {
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }

    public void setselect(int select){this.select = select;}
    public void setGroupPosition(int groupPosition){this.groupPositions = groupPosition;}

    public void setSelectedItem(int groupPosition,int selected) {
        this.parentPosition = groupPosition;
        if (old != -1) {
            this.selected.put(old, false);
        }
        this.selected.put(selected, true);
        old = selected;
    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值