我正在尝试在ExpandableListView中启用快速滚动,并且我已尝试通过此链接找到解决方法:android fastScroll only covers part of the list
问题是每当我展开足够的组来触发快速滚动条时,它就会给我NullPointerException错误。 我尝试过以下代码:
MainActivity:
expandblelist = (ExpandableListView) findViewById(R.id.mexpandablelistview);
Adapter = new Adapter(this, expandblelist, category_array);
expandblelist.setAdapter(Adapter);
expandblelist.setFastScrollEnabled(true);
Adapter:
public class Adapter extends BaseExpandableListAdapter
implements SectionIndexer, AbsListView.OnScrollListener {
private Context mContext;
private ExpandableListView mExpandableListView;
private List<Category> mGroupCollection;
private int[] groupStatus;
private boolean manualScroll;
private String[] groups;
public Adapter(Context context, ExpandableListView pExpandableListView, List<Category> pGroupCollection) {
mContext = context;
mGroupCollection = pGroupCollection;
mExpandableListView = pExpandableListView;
groupStatus = new int[mGroupCollection.size()];
setListEvent();
this.mExpandableListView.setOnScrollListener(this);
}
@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 mExpandableListView.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(section));
}
}
// Gets called when scrolling the list manually
@Override
public int getSectionForPosition(int position) {
return ExpandableListView.getPackedPositionGroup(mExpandableListView.getExpandableListPosition(position));
}
@Override
public String[] getSections() {
return groups;
}
LogCat:
NullPointerException
FastScroller.getThumbPositionForListPosition(FastScroller.java:680)
FastScroller.onScroll(FastScroller.java:487)
at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1337)
有人可以指导我吗? 非常感谢您的时间和帮助。
非常感谢
答案 0 :(得分:0)
您重写getSections
以返回String数组“groups”,但该数组似乎没有在任何地方初始化(除非它是在您尚未发布的代码中完成的)。 FastScroller中的mSections
被初始化为getSections调用的结果,因此当FastScroller尝试访问该字段时会出现NullPointerException;给你NPE的那条线是
final int sectionCount = mSections.length;