【问题标题】:ExpandableListView - How to keep just one group child highlighted?ExpandableListView - 如何只突出显示一组孩子?
【发布时间】:2016-02-25 19:22:31
【问题描述】:

如何取消选择所有其他已选择的子视图?我试图在任何给定时间只突出显示一个孩子。在下面的代码中,我可以突出显示孩子,但我在该组中选择的任何其他孩子都保持突出显示。另外,如果我选择一个孩子并切换组,相同位置的下一组孩子也会被突出显示。

 //CHILD CLICK LISTENER
 lstRoute.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

        for(int i = 0; i < parent.getChildCount(); i++)
        {
            View child = parent.getChildAt(i);
            if(child instanceof ViewGroup)
            {
                //DO NOTHING
            }else
            {
                child.setBackgroundColor(Color.TRANSPARENT);
            }
        }
        v.setBackgroundColor(Color.GREEN);
        return true;
    }
});

 //GROUP EXPANDED
lstRoute.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
    @Override
    public void onGroupExpand(int groupPosition) {

        //Only allow one group to be expanded at a time
        if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) {
            lstRoute.collapseGroup(lastExpandedPosition);
        }
        lastExpandedPosition = groupPosition;

        //Clear all selections
        for(int i = 0; i < lstRoute.getChildCount(); i++)
        {
            View child = lstRoute.getChildAt(i);
            if(child instanceof ViewGroup) {
                child.setBackgroundColor(Color.argb(100,139,213,252)); //Light Blue
            }else{
                child.setBackgroundColor(Color.TRANSPARENT);
            }
        }
    }
});

【问题讨论】:

    标签: android expandablelistview expandablelistadapter


    【解决方案1】:

    ListViews 回收视图。这意味着,如果一个视图隐藏在一个组中,它将在新打开的组中使用。

    一般来说:在ListViews 中突出显示选择不应在ClickListeners 中实现,而应在自定义Adapter 中实现。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2013-07-30
    • 2012-01-03
    相关资源
    最近更新 更多