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<>();

相关内容

热门资讯

第六软件!大熊苏州麻将诀窍(辅... 1、第六软件!大熊苏州麻将诀窍(辅助挂)太坑了其实真的有挂(有挂方式)-哔哩哔哩;详细教程。2、大熊...
九脚本(微扑克机制)外挂透明挂... 九脚本(微扑克机制)外挂透明挂辅助机制(辅助挂)透明挂教程!(有挂详情)-哔哩哔哩;建议优先通过微扑...
1个辅助器!wpk德州辅助(俱... wpk德州新手教程相关信息汇总(需添加指定薇757446909获取下载链接);1个辅助器!wpk德州...
9个机制!德扑ai软件(牌型胜... 9个机制!德扑ai软件(牌型胜率)辅助透视(有挂规律)-哔哩哔哩1、让任何用户在无需AI插件第三方神...
3个俱乐部!微扑克智能ai辅助... 1、3个俱乐部!微扑克智能ai辅助(系统机制)辅助透视(有挂辅助)-哔哩哔哩;该软件可以轻松地帮助玩...
第一机器人!广西老友摆牌十三张... 第一机器人!广西老友摆牌十三张有没有外挂(辅助挂)太坑了原来真的有挂(揭秘有挂)-哔哩哔哩;是一款可...
第四德州版(微扑克脚本)外挂透... 第四德州版(微扑克脚本)外挂透明挂辅助器(辅助挂)专业教程(确实有挂)-哔哩哔哩微扑克脚本平台为新手...
九个插件!德扑赔率胜率计算(数... 九个插件!德扑赔率胜率计算(数据分析软件)辅助透视(有人有挂)-哔哩哔哩;最新版2024是一款经典耐...
四个打法!微扑克规律(辅助器)... 四个打法!微扑克规律(辅助器)辅助透视(了解有挂)-哔哩哔哩;原来确实真的有挂(需添加指定薇7574...
8工具(aapokEr)外挂透... 8工具(aapokEr)外挂透明挂辅助工具(辅助挂)新2024教程(有挂方针)-哔哩哔哩;支持2-1...