6

我有一个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;
                }
            });
4

2 回答 2

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;
        }
    });
于 2013-02-07T10:32:58.917 回答
2

您可以对 ExpandableListView 使用 OnGroupClickListener 来限制组点击。

更新

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;
    }
});

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

于 2013-02-07T10:13:28.850 回答