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. 最后,确保为可扩展列表视图添加组项和子项的布局文件。例如,可以使用

相关内容

热门资讯

透视ai"poker... 透视ai"pokermaster脚本"详细辅助软件教程(私人局辅助挂辅助);实战中需综合运用上述技巧...
透视ai"德州局hh... 透视ai"德州局hhpoker"详细辅助靠谱教程(手机版透视脚本神器);《WPK辅助透视》‌:支持手...
透视能赢"wepok... 透视能赢"wepoker透视脚本免费app"详细辅助技巧教程(透视脚本免费透明);人气非常高,ai更...
透视辅助"aapok... 透视辅助"aapoker插件"详细辅助教你攻略(俱乐部辅助方法)1、玩家可以在aapoker插件软件...
透视ai"wepok... 这是一款非常优秀的wepoker俱乐部辅助 ia辅助检测软件,能够让你了解到wepoker俱乐部辅助...
透视实锤"WePOk... 这是一款非常优秀的WePOker有没有透视方法 ia辅助检测软件,能够让你了解到WePOker有没有...
透视辅助"wepok... 透视辅助"wepoker辅助分析器"详细辅助2025新版教程(可以设置盖牌软件)1、玩家可以在wep...
透视透视挂"wepo... 这是一款非常优秀的wepoker免费透视脚本 ia辅助检测软件,能够让你了解到wepoker免费透视...
透视肯定"wepok... 透视肯定"wepoker能不能透视"详细辅助透明挂教程(私人局规律方法)是一款可以让一直输的玩家,快...
透视安卓版"德普之星... 1、透视安卓版"德普之星私人局辅助免费"详细辅助2025新版技巧(透视app下载神器);详细教程。2...