Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

gridview - Android - OnScrollListener for Staggered Grid View

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 Grid View (https://github.com/maurycyw/StaggeredGridView)

It's work fine but... There is no OnScrollListener ! I had to know when the user see the last item, in order to load more. But it's not possible without OnScrollListener !

Have you got an idea for my problem ?

Thanks a lot.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </RelativeLayout>

</ScrollView>

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...