android ExpandableListView - 将某些组设置为不可扩展

时间:2013-02-07 09:47:51

标签: android expandablelistview

我有一个ExpandableListView,我需要设置一个特定的组(groupPosition已知且始终相同)不可扩展,并在单击时启动一个新的Activity。有没有办法做到这一点?

更新

我也只需要一次扩展一个组,并尝试以下(但这不能正常工作):

  getExpandableListView().setOnGroupClickListener(
            new OnGroupClickListener() {
                @Override
                public boolean onGroupClick(ExpandableListView parent,
                        View v, int groupPosition, long id) {
                    if (groupPosition == 1) {
                        Toast.makeText(getApplicationContext(), "TEST",
                                Toast.LENGTH_SHORT).show();
                    } else if (groupPosition != lastExpandedGroupPosition) {
                        getExpandableListView().collapseGroup(
                                lastExpandedGroupPosition);
                    } else
                        getExpandableListView().expandGroup(groupPosition);
                    lastExpandedGroupPosition = groupPosition;

                    return true;
                }
            });

2 个答案:

答案 0 :(得分:18)

这将解决这个问题 - 在onGroupClick中检查要特别处理的组,为它返回true,对于任何想要由默认操作处理的组件返回false(展开/折叠。)

    mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            if(groupPosition == 2) {
                Toast.makeText(mContext, "Group 2 clicked", Toast.LENGTH_LONG).show();
                return true;
            } else
                return false;
        }
    });

答案 1 :(得分:2)

您可以使用OnGroupClickListener for ExpandableListView来限制组点击次数。

<强>更新

private int previousGroupPosition = -1;
private boolean isDoubleTap = false;    

expLv.setOnGroupClickListener(new OnGroupClickListener()
{
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) 
    {
        if(previousGroupPosition != -1)
            expLvTodoCategories.collapseGroup(previousGroupPosition);

        if(previousGroupPosition != groupPosition || isDoubleTap)
        {
            isDoubleTap = false;
            previousGroupPosition = groupPosition;
            if(groupPosition == 1)
            {
                 //Start Activity
            }
            else
            {
                expLv.expandGroup(groupPosition);
            }
        }
        else
            isDoubleTap = true;

        return true;
    }
});

尝试上面的代码,它可能很有用..