【问题标题】:Expand one item only inside expandable item without being collapsed仅在可展开项目内展开一项而不折叠
【发布时间】:2017-01-07 00:52:03
【问题描述】:

我有一个抽屉,里面有多个可扩展项目,例如“main1”(可扩展项目)里面有“main2-1”、“main2-2”、“main2-3”(可扩展项目) "main3-1","main3-2","main3-3"(普通项目)

我只想让“main2”级别的 ExpandedItem (main2-1, 2-1, 2-3) 也在“main1”级别

我尝试过result.getAdapter().withOnlyOneExpandedItem(true);,其中“result”是我正在使用的抽屉库 - 它不起作用,因为折叠“main2”关卡而不是展开关卡的第一项

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

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

【问题讨论】:

    标签: android expand expandablelistadapter


    【解决方案1】:

    我已经做了一个方法,我把它放在扩展项目的 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;

    【讨论】:

      猜你喜欢
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多