Android 如何让FastScroll使用ExpandableListView?
我正在尝试在ExpandableListView中启用快速滚动,并尝试了此链接中的解决方案: 问题是,每当我扩展足够多的组以触发快速滚动条时,它就会给我NullPointerException错误。 我尝试了以下代码:Android 如何让FastScroll使用ExpandableListView?,android,expandablelistview,expandablelistadapter,fastscroll,Android,Expandablelistview,Expandablelistadapter,Fastscroll,我正在尝试在ExpandableListView中启用快速滚动,并尝试了此链接中的解决方案: 问题是,每当我扩展足够多的组以触发快速滚动条时,它就会给我NullPointerException错误。 我尝试了以下代码: MainActivity: expandblelist = (ExpandableListView) findViewById(R.id.mexpandablelistview); Adapter = new Adapter(this, expandblelist, catego
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)
main活动:
ExpandableList=(ExpandableListView)findViewById(R.id.mexpandablelistview);
适配器=新适配器(此,expandblelist,类别\ U阵列);
expandblelist.setAdapter(适配器);
expandblelist.setFastScrollEnabled(true);
适配器:
公共类适配器扩展了BaseExpandableListAdapter
实现节索引器AbsListView.OnScrollListener{
私有上下文;
私有可扩展列表视图mExpandableListView;
私人名单收集;
私有int[]组状态;
私人卷轴;
私有字符串[]组;
公共适配器(上下文上下文、ExpandableListView、pExpandableListView、List pGroupCollection){
mContext=上下文;
mGroupCollection=pGroupCollection;
mExpandableListView=pExpandableListView;
groupStatus=newint[mGroupCollection.size()];
setListEvent();
this.mExpandableListView.setOnScrollListener(this);
}
@凌驾
公共无效onScrollStateChanged(AbsListView视图,int scrollState){
this.manualScroll=scrollState==SCROLL\u STATE\u TOUCH\u SCROLL;
}
@凌驾
public void onScroll(AbsListView视图,
int firstVisibleItem,
int visibleItemCount,
int totalItemCount){}
@凌驾
公共int getPositionForSection(int段){
如果(手动滚动){
返回段;
}否则{
返回mExpandableListView.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(section));
}
}
//手动滚动列表时调用
@凌驾
公共int getSectionForPosition(int位置){
返回ExpandableListView.getPackedPositionGroup(mExpandableListView.getExpandableListPosition(position));
}
@凌驾
公共字符串[]getSections(){
返回组;
}
日志:
空指针异常
getThumbPositionForListPosition(fastcroller.java:680)
onScroll(fastcoller.java:487)
在android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1337)上
有人能在这方面指导我吗?
我非常感谢你的时间和帮助
非常感谢您正在覆盖
getSections
以返回字符串数组“groups”,但数组似乎没有在任何地方初始化(除非它是在您尚未发布的代码中完成的)FastScroller中的.mSections
初始化为getSections调用的结果,因此当FastScroller尝试访问该字段时,您会得到一个NullPointerException;给出NPE的行是
final int sectionCount = mSections.length;
您使用的是哪个SDK级别?我从2014年9月开始使用最新的Eclipse/ADT。请关注Android 4.3。我没有在任何其他类的任何地方初始化groups数组。我只是在我的问题帖子中的链接中遵循解决方案。显然,缺少了一些内容,我试图询问该链接中的人有一个问题,但还没有人回答。我不太明白这个解决方案是如何工作的。是否有任何方法可以提供一些示例代码?我的应用程序中没有这些代码:final int sectionCount=mSections.length;多谢了,这些代码来自Android的FastScroller类——这就是异常的来源。我不是确定应该根据您的代码初始化哪些“组”,但至少尝试在构造函数中实例化它(即
groups=newstring[0];
),并且至少应该消除该NPE。