简体   繁体   English

如何使FastScroll与ExpandableListView一起使用?

[英]How to get FastScroll to work with ExpandableListView?

I am trying to enable the Fast Scroll in ExpandableListView, and I've tried the workaround solution from this link: android fastScroll only covers part of the list 我正在尝试在ExpandableListView中启用“快速滚动”,并且我尝试过此链接中的解决方法: android fastScroll仅覆盖列表的一部分

The problem is that it gives me the NullPointerException error whenever I expand enough groups to trigger the fast scroll bar. 问题在于,每当我扩展足够的组以触发快速滚动条时,它就会给我NullPointerException错误。 I've tried the following code: 我尝试了以下代码:

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)

Can someone please guide me on this? 有人可以指导我吗? I very appreciate for your time and help. 非常感谢您的宝贵时间和帮助。

Thanks a lot 非常感谢

You are overriding getSections to return your String array "groups", but the array does not appear to be initialized anywhere (unless it is done in code that you have not posted). 您正在重写getSections以返回您的String数组“ groups”,但是该数组似乎未在任何地方初始化(除非已在未发布的代码中完成)。 mSections in FastScroller is initialized to the result of a getSections call, so you get a NullPointerException when FastScroller tries to access that field; mSections中的mSections初始化为getSections调用的结果,因此当FastScroller尝试访问该字段时,您会收到NullPointerException。 the line giving you the NPE is 给你NPE的那条线是

final int sectionCount = mSections.length;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM