在子项单击时关闭可扩展的ListView

时间:2016-06-12 17:25:34

标签: android expandablelistview

我实现了可扩展列表视图的以下方法。现在,单击父级时,listView将折叠。我想要做的是将列表设置为在按下子项时崩溃(不仅是组)。虽然我在搜索,但我无法理解我需要覆盖/实现哪种方法。谢谢!

expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
parent.collapseGroup(groupPosition);
return false;
    }
});
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

    @Override
    public void onGroupExpand(int groupPosition) {

    }
});
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

    @Override
    public void onGroupCollapse(int groupPosition) {

    }
});

expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
return false;
    }
});

3 个答案:

答案 0 :(得分:3)

您需要做的就是添加以下代码:

@Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            parent.collapseGroup(groupPosition);
            return false;
        }

答案 1 :(得分:0)

正在运作请参见截屏enter image description here

 expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            // TODO Colapse Here Using this... in android
            int previousGroup = -1;
            boolean flag = false;

            @Override
            public void onGroupExpand(int groupPosition) {

                Log.e("keshav", "onGroupClick is -> " + groupPosition);

                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();

                if (groupPosition != previousGroup && flag) {
                    expListView.collapseGroup(previousGroup);
                }
                previousGroup = groupPosition;

                flag = true;

            }
        });

请参阅此处ExpandableLists in Android, I want to allow only one parent list to expand at a time

答案 2 :(得分:0)

使用此代码折叠子视图上的ExpandableListView

@Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {

            parent.collapseGroup(groupPosition);
            return false;
        }