2011-06-06 49 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); 

希望這會幫助你。