Android - OnScrollListener for Staggered Grid View

后端 未结 4 1013
陌清茗
陌清茗 2020-12-18 15:51

Please excuse my English, I\'m French...

So, I\'ve got a question for my Android App. I\'ve to integrate a grid view as Pinterest style. I found this lib : Staggered

4条回答
  •  萌比男神i
    2020-12-18 16:34

    Try this if nothing works for you then wrap your recycler view in a scroll view and then check if scroll view has reached its bottom:

    In your xml file:

    
    
        
    
            
    
        
    
    
    

    In your class file:

    private ScrollView mScrollView;
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initScrollView();
        ...
    }
    
    private void initScrollView() {
        mScrollView = (ScrollView) mView.findViewById(R.id.scroll_view);
        mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                if (mScrollView != null) {
                    if (mScrollView.getChildAt(0).getBottom() <= (mScrollView.getHeight() + mScrollView.getScrollY())) {
                        // Scroll view is at bottom
                        // If not loading then start load more
                    } else {
                        // Scroll view is not at bottom
                    }
                }
            }
        });
    }
    

    Remember to set nested scrolling of recycler view to false for smooth scrolling in your class file:

    mRecyclerView.setNestedScrollingEnabled(false);
    

    If you want to start loading next page just before it reaches the bottomm, then just subtract an integer from mScrollView.getChildAt(0).getBottom()

    For example 1000 is subtracted here:

    if (mScrollView.getChildAt(0).getBottom() - 1000 <= (mScrollView.getHeight() + mScrollView.getScrollY())) {
        // Scroll view is at bottom
        // If not loading then start load more
    } else {
        // Scroll view is not at bottom
    }
    

    Use a greater integer to start loading much before the scroll reaches bottom.

提交回复
热议问题