折叠列表-ExpandableListView

1.1xml布局







1.2组布局



    

    

    


1.3子布局



    

    


2.适配器 extends BaseExpandableListAdapter

// 数据源
private List> data = new ArrayList<>();
private Context mContext;
// 构造方法
public ExpandableAdapter(List> data, Context mContext) {
    this.data = data;
    this.mContext = mContext;
}
// 获取组元素数目
@Override
public int getGroupCount() {
    return data.size();
}

// 获取子元素数目
@Override
public int getChildrenCount(int groupPosition) {

    // 取出组中的groupPosition位置的数据
    Map group = data.get(groupPosition);
    // 取出groupPosition位置所有子元素
    List> child = (List>) group.get("ry");

    return child.size();
}
// 获取组元素对象
@Override
public Object getGroup(int groupPosition) {
    return data.get(groupPosition);
}

// 获取子元素对象
@Override
public Object getChild(int groupPosition, int childPosition) {

    // 取出组中的groupPosition位置的数据
    Map group = data.get(groupPosition);
    // 取出groupPosition位置所有子元素
    List> child = (List>) group.get("ry");

    return child.get(childPosition);
}
// 获取组元素Id
@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

// 获取子元素Id
@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}
// 分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
@Override
public boolean hasStableIds() {
    return true;
}

// 用来判断某Group某个child是否可选
@Override
public boolean isChildSelectable(int i, int i1) {
    return true;
}
// 加载并显示组元素 
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    final Map group = data.get(groupPosition);

    GroupViewHolder groupViewHolder;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.group_item, parent, false);
        groupViewHolder = new GroupViewHolder();
        groupViewHolder.create_time = (TextView) convertView.findViewById(R.id.create_time);
        groupViewHolder.grade = (TextView) convertView.findViewById(R.id.grade);
        groupViewHolder.img = (ImageView) convertView.findViewById(R.id.img);
        convertView.setTag(groupViewHolder);
    } else {
        groupViewHolder = (GroupViewHolder) convertView.getTag();
    }

    // 展示时间
    groupViewHolder.create_time.setText((String) group.get("titleRq"));
    // 展示班级
    groupViewHolder.grade.setText((String) group.get("grade"));
    // 根据展开状态显示箭头方向
    if (isExpanded){
        groupViewHolder.img.setImageResource(R.mipmap.shangla);
    }else {
        groupViewHolder.img.setImageResource(R.mipmap.xiala);
    }

    return convertView;
}
// 获取显示指定分组中的指定子选项的视图
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    // 取出组中的groupPosition位置的数据
    Map group = data.get(groupPosition);
    // 取出groupPosition位置的子元素
    List> child = (List>) group.get("ry");

    final Map childData = child.get(childPosition);

    final ChildViewHolder childViewHolder;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.child_item, parent, false);
        childViewHolder = new ChildViewHolder();
        childViewHolder.ry_name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
    // 名字
    childViewHolder.ry_name.setText((String) childData.get("name"));

    return convertView;
}
static class GroupViewHolder {
    TextView create_time;
    TextView grade;
    ImageView img;
}

static class ChildViewHolder {
    TextView ry_name;
}

3.json数据结构


折叠列表-ExpandableListView_第1张图片
AE36A1DA-A340-4233-B23A-1C5A7E87C8EB.png

4.设置适配器(填装数据)

Map result = JSON.parseObject(getJsonstring(), new TypeReference>() {
        });
List> data = (List>) result.get("data");
adapter = new ExpandableAdapter(data,this);
ev.setAdapter(adapter);

5.长按事件

ev.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
      
        // 负数-长按子View
        final long packedPosition = ev.getExpandableListPosition(position);
        final int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
        final int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
                
        return true;
    }
});

6.默认展开所有group

for (int i = 0; i < data.size(); i++) {
    ev.expandGroup(i);
}

7.设置不可关闭group

ev.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {

        //设置点击不关闭(不收回)
        view.setClickable(true);
        return true;
    }
});

8.上拉加载

ev.setOnScrollListener(this);
public int last_index;
public int total_index;

@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
    if (last_index == total_index && (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE)) {
        // 加载下一页
        currentPage++;
        // 加载数据
        Map result = JSON.parseObject(getJsonstring(), new TypeReference>() {
                });
        List> data = (List>) result.get("data");
        // 添加到数据源
        adapter.addData(data);
    }
}

@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    last_index = firstVisibleItem + visibleItemCount;
    total_index = totalItemCount;
}

9.效果


折叠列表-ExpandableListView_第2张图片
Screenshot_20190115-104418.jpg

你可能感兴趣的:(折叠列表-ExpandableListView)