仅在可展开项目内展开一个项目而不折叠

时间:2017-01-07 00:52:03

标签: android expand expandablelistadapter

我有一个带有多个可展开物品的抽屉,里面有一个“main1”(可扩展物品),里面有“main2-1”,“main2-2”,“main2-3”(可扩展物品) “main3-1”,“main3-2”,“main3-3”(正常项目)

我想只让一个ExpandedItem处于“main2”级别(main2-1,2-1,2-3) 也是在“main1”的水平

我试过result.getAdapter().withOnlyOneExpandedItem(true);“结果”是我正在使用的抽屉库 - 它不起作用cuz折叠“main2”级别而不是扩展级别的第一项

此外,this方法在第三次点击不同的可扩展项目时不起作用 - 我使用Logging跟进代码以确保应用程序输入但是它不会折叠前一个

if(previousExpandableItemPosition != position) {
   result.getAdapter().collapse(previousExpandableItemPosition);
   previousExpandableItemPosition = position;
}

1 个答案:

答案 0 :(得分:0)

我已经制作了一个方法,我放置在扩展项目的onClick中,我想只扩展其中一个,这里是为了帮助其他人

if (position != previousPosition && previousPosition != 0 && previousIdentifier != drawerItem.getIdentifier()) { // WithOnlyOneExpandableItem
                                IDrawerItem itemx = result.getAdapter().getItem(previousPosition);
                                if (itemx.isExpanded()) {
                                    itemx.withIsExpanded(true);
                                    result.updateItem(itemx);
                                } else {
                                    if(previousPosition > position){ // user is going upward
                                        itemx = result.getAdapter().getItem(previousPosition + result.getAdapter().getItem(position).getSubItems().size());
                                        if (itemx.isExpanded()) {
                                            itemx.withIsExpanded(true);
                                            result.updateItem(itemx);
                                        }else{Log.i("ppppppppppppp", String.valueOf(previousPosition + (previousPosition - position)));}

                                    }else if(previousPosition > position){ // user is going downward
                                        itemx = result.getAdapter().getItem(previousPosition + (previousPosition
                                                + result.getAdapter().getItem(previousPosition).getSubItems().size()));
                                        if (itemx.isExpanded()) {
                                            itemx.withIsExpanded(true);
                                            result.updateItem(itemx);
                                        }else{Log.i("ppppppppppppp", String.valueOf(previousPosition + (previousPosition - position)));}
                                    }
                                }
                            } else if (previousPosition != 0 && previousIdentifier == drawerItem.getIdentifier()) {
                                //if the drawer item is collapsed after expanding then deselect it
                                EnterTimes++;
                                if(!result.getAdapter().getItem(position).isExpanded()){
                                    EnterTimes=1;
                                }
                                if(EnterTimes <2){
                                    result.getAdapter().getItem(position).withSetSelected(false);
                                    result.updateItem(drawerItem);
                                }
                            }
                            previousIdentifier = (int) drawerItem.getIdentifier();
                                previousPosition = position;

在onCreate int previousPosition = 0, previousIdentifier = 0;

之前