Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling SwipeRefreshLayout with RecyclerView Refresh anywhere in android 2.2

I have problem with my layout, I created SwipeRefreshLayout with RecyclerView inside. in android 4.2.2+ all is working good, but in andorid 2.3.4 I cant to scroll up because in any place in the RecyclerView it will refresh, and I must to scroll down and then scroll up.

This is my code:

<android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/forum_swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LVP"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center" />
</android.support.v4.widget.SwipeRefreshLayout>

I found this issue:https://code.google.com/p/android/issues/detail?id=78191 but no a solution. Any idea how to fix it?

like image 601
Haim127 Avatar asked Dec 24 '14 19:12

Haim127


2 Answers

override RecyclerView's method OnScrollStateChanged

 mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                // TODO Auto-generated method stub
                //super.onScrollStateChanged(recyclerView, newState);
               try {
                   int firstPos = mLayoutManager.findFirstCompletelyVisibleItemPosition();
                   if (firstPos > 0) {
                       mSwipeRefreshLayout.setEnabled(false);
                   } else {
                       mSwipeRefreshLayout.setEnabled(true);
                       if(mRecyclerView.getScrollState() == 1)
                           if(mSwipeRefreshLayout.isRefreshing())
                            mRecyclerView.stopScroll();
                   }

               }catch(Exception e) {
                   Log.e(TAG, "Scroll Error : "+e.getLocalizedMessage());
               }
            }

Check if Swipe Refresh is Refreshing and try to Scroll up then you got error, so when swipe refresh is going on and i try do this mRecyclerView.stopScroll();

like image 77
Mansukh Ahir Avatar answered Oct 16 '22 19:10

Mansukh Ahir


I fixed the scroll up issue using the following code :

private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        LinearLayoutManager manager = ((LinearLayoutManager)recyclerView.getLayoutManager());
        boolean enabled =manager.findFirstCompletelyVisibleItemPosition() == 0;
        pullToRefreshLayout.setEnabled(enabled);
    }
};

Then you need to use setOnScrollListener or addOnScrollListener depending if you have one or more listeners.

like image 45
emaleavil Avatar answered Oct 16 '22 20:10

emaleavil