ExpandabeListView自动崩溃

时间:2011-05-18 10:37:25

标签: android

我无法在扩展新群组时关闭之前扩展的群组。我读过类似的问题,但我无法从中提取概念。我尝试使用collapseGroup(groupPosition)方法,但它无效。

1 个答案:

答案 0 :(得分:3)

您好请尝试以下代码。


int lastExpandedGroupPosition = 0;   

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);

    listView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {

            // TODO Auto-generated method stub
            if (groupPosition != lastExpandedGroupPosition) {
                listView.collapseGroup(lastExpandedGroupPosition);

            }
            lastExpandedGroupPosition = groupPosition;

        }
    });

    adapter = new ExpandableListAdapter(this, new ArrayList<String>(),
            new ArrayList<ArrayList<Vehicle>>());

    // Set this blank adapter to the list view
    listView.setAdapter(adapter);


}

在线下导入。


import android.widget.ExpandableListView.OnGroupExpandListener;