How to refresh SwipeRefreshLayout with two views inside
Asked Answered
W

2

5

I have SwipeRefreshLayout with two child : RecyclerView And LinearLayout So i had to customize RecyclerView :

recycler_view.setOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            // int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
            // swipeRefreshLayout.setEnabled(topRowVerticalPosition >= 0);
            if (!defined) {
                toolbarHeight = ll_header.getMeasuredHeight() + 15;
                footerHeight = ll_footer.getMeasuredHeight() + 15;
                defined = true;
            }
            clipToolbarOffset();
            if ((toolbarOffset < toolbarHeight && dy > 0) || (toolbarOffset > 0 && dy < 0)) {
                toolbarOffset += dy;
            }
            if ((footerOffset < footerHeight && dy > 0) || (footerOffset > 0 && dy < 0)) {
                footerOffset += dy;
            }
            onMoved(toolbarOffset, footerOffset);
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            // super.onScrollStateChanged(recyclerView, newState);
            if (recyclerView.getAdapter() != null) {
                if (recyclerView.getAdapter().getItemCount() > 0) {
                    int currentPosition = ((RecyclerViewAdapter) recyclerView.getAdapter()).getCurrentPosition();
                    if (currentPosition > limit && currentPosition >= items.size() - 2) {
                        if (!isLoading) {
                            getContent();
                        }
                    }
                }
            }
            LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
            if (recyclerView.getAdapter() == null) {
                swipeRefreshLayout.setEnabled(true);
                return;
            }
            swipeRefreshLayout.setEnabled(llm.findFirstCompletelyVisibleItemPosition() == 0);
        }
    });

How i can Enable refresh of SwipeRefreshLayout when LinearLayout pulling Down ?

Wiley answered 9/12, 2015 at 8:46 Comment(0)
Z
11

If you want to enable SwipeRefresh on a LinearLayout you need to put the LinearLayout into a ScrollView:

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            // insert layout components here ...

        </LinearLayout>
    </ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
Zulema answered 1/1, 2016 at 22:23 Comment(0)
F
1

Try this code snippet

recycler_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

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

                swipeRefreshLayout.setEnabled(llm.findFirstCompletelyVisibleItemPosition() == 0);
            }
        });
Flourishing answered 9/12, 2015 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.