使用StaggeredLayoutManager无限滚动?

时间:2015-07-28 04:45:20

标签: android android-recyclerview

我尝试实现交错的RecyclerView。我创建Abstract onScroll RecyclerView像这样:

@Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        int grid_column_count = context.getResources().getInteger(R.integer.grid_column_count);
        StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(grid_column_count, StaggeredGridLayoutManager.VERTICAL);
        int visibleItemCount = recyclerView.getChildCount();
        int totalItemCount = mLayoutManager.getItemCount();
        int firstVisibleItemsGrid[] = new int[grid_column_count];
        int firstVisibleItem = mLayoutManager.findFirstVisibleItemPositions(firstVisibleItemsGrid)[0];


        if ((visibleItemCount + firstVisibleItem ) >= totalItemCount
                && totalItemCount != 0) {
            onLoadingMore();
        }

    }


protected abstract void onLoadingMore();

我通过MainActity实现

//===============    
recyclerview= (RecyclerView)rootView. findViewById(R.id.grid_view);
            recyclerview.setHasFixedSize(true);
            grid_column_count = getResources().getInteger(R.integer.grid_column_count);
            mLayoutManager = new StaggeredGridLayoutManager(grid_column_count, StaggeredGridLayoutManager.VERTICAL);
            recyclerview.setLayoutManager(mLayoutManager);

//=====================

recyclerview.addOnScrollListener(new HidingScrollListener(getActivity()) {

            @Override
            protected void onLoadingMore() {
                if ( isFinishLoadingAwal
                        && !isFinishMoreNews
                        && adapter.getItemCount()> 0) {
                    getMoreNewsFromServer();
                }
            }

        });

但我收到了错误:

firstVisibleItem = mLayoutManager.findFirstVisibleItemPositions(firstVisibleItemsGrid)[0];

日志:

Process: com.ad.kamardagang, PID: 23350
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v7.widget.OrientationHelper.getStartAfterPadding()' on a null object reference
        at android.support.v7.widget.StaggeredGridLayoutManager$Span.findOneVisibleChild(StaggeredGridLayoutManager.java:2337)
        at android.support.v7.widget.StaggeredGridLayoutManager$Span.findFirstVisibleItemPosition(StaggeredGridLayoutManager.java:2313)
        at android.support.v7.widget.StaggeredGridLayoutManager.findFirstVisibleItemPositions(StaggeredGridLayoutManager.java:826)
        at com.ad.kamardagang.utils.HidingScrollListener.onScrolled(HidingScrollListener.java:90)
        at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3674)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2824)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3011)
        at android.view.View.layout(View.java:15671)

那么如何解决呢?或任何解决方案? 对不起我的英文。

2 个答案:

答案 0 :(得分:2)

我使用此Mugen通过RecyclerView无限滚动StaggeredGrid。

答案 1 :(得分:-1)

firstVisibleItem = mLayoutManager.findFirstVisibleItemPositions(firstVisibleItemsGrid)[0];替换为

 firstVisibleItem  = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); and try.