扩展列表视图需要一次扩展一行

时间:2012-06-21 05:21:46

标签: android android-listview expandablelistview

即时通讯在我的项目中使用扩展列表视图,我需要一次只展开一个列表视图,即我展开一个项目,当我尝试展开列表中的另一个项目时,我扩展的上一个项目必须折叠和新项目点击必须扩展,Answers将不胜感激,我正在尝试使用下面的片段为点击监听器..

 convertViewpar.setOnClickListener(new OnClickListener() {

    int i=1;
    public void onClick(View v) {
    i = i++;
    int[] expds = new int[100];
    expds[0]=0;
    expds[i] = groupPosition;
    if(expds[i]==expds[i-1]){
    if(isExpanded)
    expandlist.collapseGroup(i);
    else
    expandlist.expandGroup(i);
    }else{
    expandlist.collapseGroup(i-1);
     if(isExpanded)
        expandlist.collapseGroup(i);
    else
    expandlist.expandGroup(i);

    }


}
}); 

2 个答案:

答案 0 :(得分:25)

在类级别和onCreate方法中添加 implements OnGroupExpandListener

    listView.setOnGroupExpandListener(this);

并添加此方法

/*
 * (non-Javadoc)
 * 
 * @see
 * android.widget.ExpandableListView.OnGroupExpandListener#onGroupExpand
 * (int)
 */
public void onGroupExpand(int groupPosition) {
    int len = expListAdapter.getGroupCount();

    for (int i = 0; i < len; i++) {
        if (i != groupPosition) {
            listView.collapseGroup(i);
        }
    }
}

它肯定会有用。

答案 1 :(得分:3)

试试这个解决方案。使用GroupClcik Listener并检查当前单击的位置并拼写所有其他组

 expList.setOnGroupClickListener(new OnGroupClickListener() {

        public boolean onGroupClick(ExpandableListView arg0, View arg1, int arg2,
                long arg3) {
            int count =  yourAdapter.getGroupCount();
            for (int i = 0; i <count ; i++)
              if(arg2!=i)
                exp.collapseGroup(i);
              else
                  exp.expandGroup(i);
            return false;
        }
    });

或者根据此解决方案,Programmatically collapse a group in ExpandableListView

@Override
public void onGroupExpanded(int groupPosition){
    //collapse the old expanded group, if not the same
    //as new group to expand
    if(groupPosition != lastExpandedGroupPosition){
        accordion.collapseGroup(lastExpandedGroupPosition);
    }

    super.onGroupExpanded(groupPosition);           
    lastExpandedGroupPosition = groupPosition;
}