BaseExpandableListAdapter重写新项覆盖旧项。
创始人
2024-11-25 09:00:13
0

在Android中,可以使用BaseExpandableListAdapter来创建可扩展的列表适配器。重写新项覆盖旧项的解决方法如下所示:

  1. 创建一个自定义的ExpandableListAdapter类,继承自BaseExpandableListAdapter。
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

    // 定义数据源
    private List groupList;
    private Map> childMap;

    // 构造方法
    public CustomExpandableListAdapter(List groupList, Map> childMap) {
        this.groupList = groupList;
        this.childMap = childMap;
    }

    // 获取组的数量
    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    // 获取组在指定位置的数据
    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    // 获取组的ID
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // 获取指定组的子项数量
    @Override
    public int getChildrenCount(int groupPosition) {
        String group = groupList.get(groupPosition);
        return childMap.get(group).size();
    }

    // 获取子项数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String group = groupList.get(groupPosition);
        return childMap.get(group).get(childPosition);
    }

    // 获取子项ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // 检查是否有稳定的ID
    @Override
    public boolean hasStableIds() {
        return true;
    }

    // 获取指定位置的组视图
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        // 创建或重用视图
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.group_item_layout, parent, false);
        }

        // 设置组视图的数据
        TextView groupTextView = convertView.findViewById(R.id.groupTextView);
        groupTextView.setText((String) getGroup(groupPosition));

        return convertView;
    }

    // 获取指定位置的子项视图
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        // 创建或重用视图
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_item_layout, parent, false);
        }

        // 设置子项视图的数据
        TextView childTextView = convertView.findViewById(R.id.childTextView);
        childTextView.setText((String) getChild(groupPosition, childPosition));

        return convertView;
    }

    // 检查子项是否可选
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
  1. 创建group_item_layout.xml和child_item_layout.xml文件,用于设置组视图和子项视图的布局。

group_item_layout.xml:


child_item_layout.xml:


  1. 在Activity或Fragment中使用自定义的ExpandableListAdapter。
public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    private CustomExpandableListAdapter expandableListAdapter;
    private List groupList;
    private Map> childMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化数据源
        groupList = new ArrayList<>();

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...