2010-12-16 52 views
18

我有一個類實現expandable list activity
在XML代碼中(或者我可以在java中完成),我將fastScrollEnabled設置爲true。這可以確保快速滾動。但快速滾動只能在列表的頂部顯示。就像我可以使用fastscroll thumb bar滾動整個列表,但只能在滾動條的頂部部分中工作。這與整個列表不相稱。我可以將拇指條拖動到列表的底部,但它不會滾動,因爲listview已經滾動到底部了,因爲它的奇怪行爲只能在列表的頂部顯示。
混淆我知道,我可以嘗試,如果需要澄清更多....android fastScroll只覆蓋部分列表

我實現一個自定義BaseExpandableListAdapter.

+0

列表中的行是否有不同的大小? – 2011-04-06 23:11:32

+6

你能....請........張貼您的XML – Rockin 2011-05-25 06:09:51

+5

張貼您的代碼/ xml – 2012-04-03 21:48:01

回答

1

這是在快速滾動的錯誤。它與ExpandableListView並不配合。

請參閱my code

(也包括某些情況下,要解決此問題)

+0

我下載了您的FastScroller.java,但我不知道如何實現它。你能指導我如何在我的ExpandableListView應用程序中實現它嗎?非常感謝你的代碼。 – Dante 2014-10-25 21:04:15

5

我只是找到了一個解決辦法,以防止系統顯示此錯誤的行爲。

有兩種方案使用SectionIndexer不同的代碼來工作。

第一種情況是您使用FastScrollbar-Thumb導航到下一部分的情況。假設羣體是你的部分的重寫的方法實現SectionIndexer看起來就像是:

@Override 
public int getPositionForSection(int section) { 
    return section; 
} 

// Gets called when scrolling the list manually 
@Override 
public int getSectionForPosition(int position) { 
    return ExpandableListView.getPackedPositionGroup(
       expandableListView 
        .getExpandableListPosition(position)); 
} 

第二種情況是,你手動滾動列表中的情況和快速滾動條移動根據部分,不所有項目。因此,代碼看起來像這樣:

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

// Gets called when scrolling the list manually 
@Override 
public int getSectionForPosition(int position) { 
    return ExpandableListView.getPackedPositionGroup(
       expandableListView 
        .getExpandableListPosition(position)); 
} 

人們可以看到這兩種行爲都離不開進一步普及一起玩。

使兩者都起作用的解決方法是在每個人滾動一次(即通過觸摸進行滾動)的情況下處理這種情況。這可以實現OnScrollListener接口與適配器類來完成,並將其設置到ExpandableListView

public class MyExpandableListAdapter extends BaseExpandableListAdapter 
       implements SectionIndexer, AbsListView.OnScrollListener { 

    // Your fields here 
    // ... 
    private final ExpandableListView expandableListView; 
    private boolean manualScroll; 

    public MyExpandableListAdapter(ExpandableListView expandableListView 
            /* Your other arguments */) { 
     this.expandableListView = expandableListView; 
     this.expandableListView.setOnScrollListener(this); 
     // Other initializations 
    } 

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     this.manualScroll = scrollState == SCROLL_STATE_TOUCH_SCROLL; 
    } 

    @Override 
    public void onScroll(AbsListView view, 
         int firstVisibleItem, 
         int visibleItemCount, 
         int totalItemCount) {} 

    @Override 
    public int getPositionForSection(int section) { 
     if (manualScroll) { 
      return section; 
     } else {    
      return expandableListView.getFlatListPosition(
         ExpandableListView.getPackedPositionForGroup(section)); 
     } 
    } 

    // Gets called when scrolling the list manually 
    @Override 
    public int getSectionForPosition(int position) { 
     return ExpandableListView.getPackedPositionGroup(
        expandableListView 
         .getExpandableListPosition(position)); 
    } 

    // Your other methods 
    // ... 
} 

那固定的bug我。

+0

哇,這修正了我們有3年的錯誤。謝謝! – 2013-11-23 16:34:03

+0

@ mitch000001,我把你的代碼放在我的ExpandableListView應用程序中,但是當我擴展任何組時,它給了我一個NullPointerException強制關閉錯誤。我是否需要SectionIndexer.java才能使其工作?謝謝 – Dante 2014-10-25 21:02:26