2011-06-06 52 views
8

有没有人有任何运气适应PinnedHeaderListView,以便它可以使用ExpandableListView而不是一个简单的ListView与索引部分?我基本上想要一个ExpandableListView,其中每个组项目视图保持固定在顶部,直到它被下一个组视图向上推。如何在ExpandableListView中获取粘贴/固定标题?

我已经研究了代码来试图找出PinnedHeaderListView是如何工作的,它似乎很难适应ExpandableListView。主要的问题似乎在于使用不同类型的适配器和绘图方法。 A PinnedHeaderListView利用SectionIndexer来跟踪剖面位置。由于它使用getView()绘制每个项目,它会检查项目是否是新节的开始。如果该项目是新节的开头,则会使节标题可见该项目的list_item视图中。 ExpandableListAdapter有一个getChildView()和一个getGroupView()作为不同的列表项目分别绘制项目和部分。

我确定必须有某种方法使用PinnedHeaderListView中的方法来获得ExpandableListView中的类似行为,但我不确定从哪里开始。

+0

由于@joecan没有发布整个代码,我会这样做。[我的解决方案](http://stackoverflow.com/q/10613552/1376402) – 2012-05-25 11:01:30

回答

4

我能够在ExpandableList1 APIdemo上获得固定标题。

我遇到的最难的问题是搞清楚如何让SectionIndexer与扩展列表很好地配合。正如我的问题所证明的那样,我认为这一切都是错误的。在我最初尝试解决这个问题的过程中,我在MyExpandableAdapter中创建了一个SectionIndexer对象,并将其映射到我的数据,就像它在Contacts应用程序和Peter的示例中所做的一样。这适用于通讯录应用程序,因为平面列表位置静态匹配数据集。在可扩展列表视图中,扁平列表位置随着组扩展进出而发生变化。

因此,解决方案是不要将部分索引器映射到数据,而是映射到ExpandableListView的实例。有了这个解决方案,您甚至不需要像示例中使用的SectionIndexer对象。你只需要环绕ExpandableListView方法,像这样的SectionIndexer实施方法:

@Override 
    public int getPositionForSection(int section) { 
     return mView.getFlatListPosition(ExpandableListView 
       .getPackedPositionForGroup(section)); 
    } 

    @Override 
    public int getSectionForPosition(int position) { 
     return ExpandableListView.getPackedPositionGroup(mView 
       .getExpandableListPosition(position)); 
    } 

当然还有你必须做出让这一切工作的其他变化,但上述方法是关键。我会将完整的代码发布到谷歌代码或github,并很快从这里链接。固定标题的ExpandableLists看起来不错!

+0

真棒!非常感谢你! – rekaszeru 2011-10-21 09:21:50

+1

如果你可以制作一个图书馆项目,并在github上提供它,这将是很好的。如果你需要一个模型,请参阅https://github.com/triposo/barone – aleb 2012-04-27 00:33:12

+0

@joecan你可以发布你的代码的github链接 – sankar 2012-05-15 08:27:50

2

嗯,我在短短步骤都做:

添加compile 'com.diegocarloslima:fgelv:[email protected]'的build.gradle

2.添加ExpandableListView XML格式。

<com.diegocarloslima.fgelv.lib.FloatingGroupExpandableListView 
     android:id="@+id/expandableListView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/transparent" 
     android:groupIndicator="@null" /> 

Java代码:

FloatingGroupExpandableListView expandableListView = (FloatingGroupExpandableListView) findViewById(R.id.expandableListView); 
MyexpandableListAdapter adapter = new MyexpandableListAdapter(context, mList); 
WrapperExpandableListAdapter wrapperAdapter = new WrapperExpandableListAdapter(adapter); 
expandableListView.setAdapter(wrapperAdapter); 

希望这会帮助你。