【问题标题】:Expand all children in expandable list view在可展开的列表视图中展开所有子项
【发布时间】:2011-10-15 22:41:07
【问题描述】:

我想在填充可展开列表视图时展开所有子项。 目前我的代码如下所示:

ExpandableListView listView = (ExpandableListView) findViewById(R.id.view);
int count = viewAdapted.getGroupCount();
for (int position = 1; position <= count; position++)
    listView.expandGroup(position - 1);

这很丑陋。 有更好的方法吗?

【问题讨论】:

标签: android expandablelistview


【解决方案1】:

您可以在自定义适配器的 getGroupView 中展开它:

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);
    ExpandableListView mExpandableListView = (ExpandableListView) parent;
    mExpandableListView.expandGroup(groupPosition);
    return v;
}

幸运!

【讨论】:

  • 这使它们无法点击
  • @DavidLawson:是的,我的解决方案是固定扩展它们。如果你想展开它们并且它们是可点击的,你必须处理创建 ExpandableListView 的地方,就像 Drejc 的解决方案一样。
【解决方案2】:

将此代码放在您的适配器上将使扩展列表保持打开状态,并禁用其关闭功能。

ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition);

这可能是一个小问题,但它应该是这样的。它打开所有组(在开始时)并且可以随时关闭

 for ( int i = 0; i < groupList.getCount(); i++ ) {
    groupList.expandGroup(i);
 } 

注意: 在您的可扩展视图上设置适配器后放置此代码,否则可能会导致错误。 希望能帮助到你。

【讨论】:

  • setAdapter 之前或之后放置此代码并不重要。但是你应该把它放在你为适配器设置数据之后。如果您设置适配器然后通过网络加载数据,这很有意义
【解决方案3】:

先填充适配器,然后将此代码放入您的 oncreate 方法中

   int count = adapter.getGroupCount();
                for ( int i = 0; i < count; i++ ) 
                    listView.expandGroup(i);

【讨论】:

    【解决方案4】:

    扩展所有组

    for(int i=0; i < myAdapter.getGroupCount(); i++)
        myExpandableListView.expandGroup(i);
    

    如果您想让它们不可折叠。

    myExpandableListView.setOnGroupClickListener(new OnGroupClickListener() {
      @Override
      public boolean onGroupClick(ExpandableListView parent, View v,int  groupPosition, long id) { 
        return true; 
      }
    });
    

    【讨论】:

      【解决方案5】:

      我已经尝试了列出的响应,但我发现我可以使用getGroupCount() 方法来获取组数。

      使用这种方法,我可以迭代和扩展我的 ExpandableListView 的每一组

      for (int i = 0; i < myExpandableListView.getExpandableListAdapter().getGroupCount(); i++) {
            //Expand group
            myExpandableListView.expandGroup(i);
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-31
        • 1970-01-01
        • 2011-10-14
        • 2011-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多