Android可扩展列表视图:组项无法展开/折叠
创始人
2024-10-08 17:02:19
0

要解决Android可扩展列表视图中组项无法展开/折叠的问题,可以根据以下步骤进行操作:

  1. 首先,在XML布局文件中将可扩展列表视图添加到布局中。例如,可以使用以下代码将可扩展列表视图添加到LinearLayout中:


    


  1. 在Java文件中,首先定义可扩展列表的数据源。创建一个ExpandableListViewAdapter来设置可扩展列表视图的适配器。例如,可以使用以下代码:
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List groupItems;
    private Map> childItems;

    public ExpandableListViewAdapter(Context context, List groupItems, Map> childItems) {
        this.context = context;
        this.groupItems = groupItems;
        this.childItems = childItems;
    }

    @Override
    public int getGroupCount() {
        return groupItems.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childItems.get(groupItems.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupItems.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childItems.get(groupItems.get(groupPosition)).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 false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.group_item_layout, parent, false);
        }

        TextView groupItemTextView = convertView.findViewById(R.id.groupItemTextView);
        groupItemTextView.setText((String) getGroup(groupPosition));

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.child_item_layout, parent, false);
        }

        TextView childItemTextView = convertView.findViewById(R.id.childItemTextView);
        childItemTextView.setText((String) getChild(groupPosition, childPosition));

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
  1. 接下来,在Activity中初始化可扩展列表视图并设置适配器。例如,可以使用以下代码:
public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    private ExpandableListViewAdapter expandableListViewAdapter;

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

        expandableListView = findViewById(R.id.expandableListView);

        // 初始化数据源
        List groupItems = new ArrayList<>();
        groupItems.add("Group 1");
        groupItems.add("Group 2");

        Map> childItems = new HashMap<>();
        List childItemsGroup1 = new ArrayList<>();
        childItemsGroup1.add("Child 1.1");
        childItemsGroup1.add("Child 1.2");

        List childItemsGroup2 = new ArrayList<>();
        childItemsGroup2.add("Child 2.1");
        childItemsGroup2.add("Child 2.2");

        childItems.put(groupItems.get(0), childItemsGroup1);
        childItems.put(groupItems.get(1), childItemsGroup2);

        // 设置适配器
        expandableListViewAdapter = new ExpandableListViewAdapter(this, groupItems, childItems);
        expandableListView.setAdapter(expandableListViewAdapter);
    }
}
  1. 最后,确保为可扩展列表视图添加组项和子项的布局文件。例如,可以使用

相关内容

热门资讯

透视有挂!hhpoker可以控... 透视有挂!hhpoker可以控制牌吗,德普之星app安卓版破解版,手筋教程(真是是有挂)-哔哩哔哩1...
透视最新版!约局吧德州有挂吗(... 透视最新版!约局吧德州有挂吗(透视)开挂辅助app(一贯有挂)-哔哩哔哩1、下载好约局吧德州有挂吗辅...
透视数据!pokerrrr2辅... 透视数据!pokerrrr2辅助,wepoker私人局怎么玩,学习教程(一贯是真的挂)-哔哩哔哩1、...
透视透视挂!wepoker怎么... 透视透视挂!wepoker怎么提高运气,欢乐达人暗堡链接辅助,一直真的有挂(专业力荐教程)-哔哩哔哩...
透视检测!aa poker辅助... 透视检测!aa poker辅助包(透视)开挂辅助技巧(原来真的是有挂)-哔哩哔哩1、aa poker...
透视工具!pokemmo脚本辅... 透视工具!pokemmo脚本辅助下载,德扑之心免费透视,学习教程(一直存在有挂)-哔哩哔哩1)pok...
透视ai代打!wepoker轻... 透视ai代打!wepoker轻量版有透视吗,牵手游戏辅助,一贯是有挂(了解必备教程)-哔哩哔哩暗藏猫...
透视规律!wepoker有用吗... 透视规律!wepoker有用吗(透视)开挂辅助器(切实真的是有挂)-哔哩哔哩透视规律!wepoker...
透视挂!pokerworld破... 透视挂!pokerworld破解版下载,哈糖大菠萝破解器,秘籍教程(确实真的有挂)-哔哩哔哩1、不需...
受玩家影响!aapoker插件... 受玩家影响!aapoker插件,白银胡乐辅助脚本最新版安装方法,本来有挂(关于必备教程)-哔哩哔哩亲...