ExpandableLists in Android, I want to allow only one parent list to expand at a time

后端 未结 8 1686
难免孤独
难免孤独 2021-02-04 17:15

I want to make only one item in the parent list expand at a time, my code for the onCreate is currently the following. (It works as I want, But the method to allow only one pare

8条回答
  •  终归单人心
    2021-02-04 17:44

    The problem was solved by creating a public explist variable called list, and then referencing it to the adapter in view with the following call.

    list = getExpandableListView(); 
    

    The expand method before was crashing the app because of this. After refferencing the list.collapsegroup in, it worked fine.

    public void onGroupExpand(int groupPosition) { 
                        System.out.println("GROUP LISTENER WORKS?");
                        if(adptr=="expListAdapter"){
                            int len = expListAdapter.getGroupCount();
                            //System.out.println(len);
                            for (int i = 0; i < len; i++) {
                                if (i != groupPosition) {
                                list.collapseGroup(i);
                                }
                            }
    
                        }
                        else if(adptr=="expListAdapter2"){
                            int len = expListAdapter2.getGroupCount();
                            for (int i = 0; i < len; i++) {
                                if (i != groupPosition) {
                                list.collapseGroup(i);
                                }
                            }
                            //System.out.println(len);
                        }
                        else if(adptr=="expListAdapter3"){
                            int len = expListAdapter3.getGroupCount();
                            for (int i = 0; i < len; i++) {
                                if (i != groupPosition) {
                                list.collapseGroup(i);
                                }
                            }
    
                        }
                }
    

提交回复
热议问题