如何让FastScroll使用ExpandableListView?
我试图启用ExpandableListView快速滚动,我已经试过此链接中的解决方法解决:android fastScroll only covers part of the list如何让FastScroll使用ExpandableListView?
的问题是,它给了我的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)
有人可以请指导我吗? 我非常感谢您的时间和帮助。
非常感谢
要覆盖getSections
来回报您的字符串数组“团体”,但数组不会出现在任何地方进行初始化(除非它是在你没有张贴代码完成)。 FastScroller中的mSections
被初始化为getSections调用的结果,所以当FastScroller尝试访问该字段时会得到NullPointerException;给你NPE的线路是
final int sectionCount = mSections.length;
感谢您的评论。我没有在任何其他类中的任何位置初始化组数组。我只是在我的问题帖子中关注链接中的解决方案。很显然,有些东西缺失了,我试图向这个环节中的人提出一个问题,但没有人还没有回答。我不太明白这个解决方案的解决方案是如何工作的。有无论如何可以提供一些示例代码?我没有在我的应用程序代码:final int sectionCount = mSections.length;非常感谢 – Dante 2014-11-19 19:14:00
该代码来自Android的FastScroller类 - 这是异常来自的地方。我不确定应该根据你的代码初始化什么“组”,但至少应该在你的构造函数中实例化它(即'groups = new String [0];'),它至少应该消除NPE。 – EricaCooksey 2014-11-19 20:39:54
您使用哪个SDK级别? – EricaCooksey 2014-11-18 19:47:57
自2014年9月起,我使用最新的Eclipse/ADT。目标Android 4.3 – Dante 2014-11-18 20:19:17