我有一个实现expandable list activity
的类。
在XML代码中(或者我可以在java中这样做),我将fastScrollEnabled
设置为true
。这确实可以实现快速滚动。但是快速滚动只适用于列表的顶部。例如,我可以使用快速滚动拇指条来滚动整个列表,但只在滚动条的顶部有效。它与整个列表不成比例。我可以拖动拇指条到列表的底部,但它不会滚动,因为listview
已经滚动到底部,因为它的奇怪行为只在列表的顶部工作。
令人困惑的是,我知道,如果需要,我可以尝试澄清更多....
我实现了一个自定义的BaseExpandableListAdapter.
发布于 2013-06-24 22:26:04
我刚刚找到了一种解决方法,可以防止系统显示这种错误行为。
有两种情况使用不同的代码使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。
发布于 2012-04-29 20:47:21
https://stackoverflow.com/questions/4461804
复制相似问题