gpt4 book ai didi

android fastScroll 只覆盖列表的一部分

转载 作者:IT王子 更新时间:2023-10-28 23:32:35 24 4
gpt4 key购买 nike

我有一个实现可扩展列表 Activity 的类。
在 XML 代码中(或者我可以在 java 中完成),我将 fastScrollEnabled 设置为 true。这确实可以实现快速滚动。但快速滚动仅适用于列表的顶部。就像我可以使用 fastscroll thumb bar 滚动整个列表,但只能在滚动条的顶部工作。它与整个列表不成比例。我可以将拇指栏拖到列表的底部,但它不会滚动,因为 listview 已经滚动到底部,因为它的奇怪行为只在列表的顶部工作。
我知道很困惑,如果需要,我可以尝试澄清更多......

我确实实现了一个自定义 BaseExpandableListAdapter。

最佳答案

我刚刚找到了一种解决方法来防止系统显示此错误行为。

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 接口(interface)并将其设置到 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
// ...
}

这为我修复了错误。

关于android fastScroll 只覆盖列表的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4461804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com