【问题标题】:How do I get index of first/last visible group in an ExpandableListView?如何在 ExpandableListView 中获取第一个/最后一个可见组的索引?
【发布时间】:2010-08-29 08:30:59
【问题描述】:

如何获取 ExpandableListView 中第一个/最后一个可见组的索引?

getFirstVisiblePosition() 和 getLastVisiblePosition() 对于 ExpandableListViews 几乎没有用处,因为它们返回列表中第一个/最后一个可见 单元格 的索引。这会有所不同,因为展开的组算作多个单元格。

我需要的是一些方法,如 getFirstVisibleGroupIndex()、getLastVisibleGroupIndex() 或一些将“可见单元格索引”值从上述方法转换为实际组(+子)索引值的方法。

注意:如果在 ExpandableListView 上设置监听器,OnScrollListener.onScroll(..., int firstVisibleItem, int visibleItemCount, ...) 也会遇到同样的问题。

【问题讨论】:

    标签: android


    【解决方案1】:

    你在寻找这样的东西吗?

    public void listVisibleRowsForExpandableGroup()
    {
        int firstVis  = getFirstVisiblePosition();
        int lastVis = getLastVisiblePosition();
    
        int count = firstVis;
    
        while (count <= lastVis)
        {
            long longposition = getExpandableListPosition(count);
            int type = getPackedPositionType(longposition);
            if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                int groupPosition = getPackedPositionGroup(longposition);
                int childPosition = getPackedPositionChild(longposition);
                Log.d("Test","group: " + groupPosition + " and child: " + childPosition );
            }
            count++;
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      我知道这个问题很老了,但是对于像我一样偶然发现它的每个人...... 根据 sara 的回答,这是我现在使用的方法:

          public int getFirstVisibleGroup() {
                  int firstVis = list.getFirstVisiblePosition();
              long packedPosition = list.getExpandableListPosition(firstVis);
              int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
              return groupPosition;
      }
      

      getLastVisiblePosition() 也应该这样...

      【讨论】:

        猜你喜欢
        • 2012-03-13
        • 2015-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-16
        • 2020-01-05
        • 2023-04-09
        • 2021-06-17
        相关资源
        最近更新 更多