Disable collapsing of ExpandableListView

2019-04-18 11:43发布

The default behavior of ExpandableListView is to collapse a group when its header is clicked. Is it possible to prevent this from happening?

I've tried:

  • Setting OnTouchListener on the list. This interferes with scrolling.
  • Setting an OnGroupClickListener on the list (in onCreate()). This works for all clicks after the first.

Has anyone else accomplished this? Why might the OnGroupClickListener miss the first click?

Thanks in advance for your suggestions.

4条回答
霸刀☆藐视天下
2楼-- · 2019-04-18 12:27

Maybe It`s too late,I use onGroupCollapseListener in the activity and implement onGroupCollapse Method.(expandView is ExpandableListView)

@Override
public void onGroupCollapse(int collapseIndex) {
    expandView.expandGroup(collapseIndex);
}
查看更多
【Aperson】
3楼-- · 2019-04-18 12:28

You can ignore click on group items like this:

mMyExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
  @Override
  public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
      // Doing nothing
    return true;
  }
});
查看更多
Ridiculous、
4楼-- · 2019-04-18 12:30

You can use this code on the item for which you want to disable clicking on: {convertView.setClickable(false);}

查看更多
霸刀☆藐视天下
5楼-- · 2019-04-18 12:34

It seems the problem had to do with there being both a focusable element in the group header and an OnGroupClickListener set. Removing the listener solved my problem.

查看更多
登录 后发表回答