Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwipeRefreshLayout loader not going up when pull down android

I have used SwipeRefreshLayout with recyclerview, Its working fine in all android version but in Kitkat when i pull down then SwipeRefreshLayout loader not going up and also doesn't refresh recyclerview data but working in lollipop

I am using 'com.android.support:support-v4:22.2.1'

Edit 1

SwipeRefreshLayout swipe_refresh;
swipe_refresh = (SwipeRefreshLayout)rootView.findViewById(R.id.swipe_refresh);

swipe_refresh.post(new Runnable() {
            @Override
            public void run() {
                swipe_refresh.setRefreshing(true);
            }
        });


swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Loading more data..
                .....
                ....
                getData();

            }
        });

swipe_refresh.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

public void getData(){
    // Load data from web service...
    ...
    ...
    ...


    // After loading
    swipe_refresh.setRefreshing(false);

    // Set data to adapter
}

Layout Code

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@color/grey_100">

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/gv_feeds"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="9"
                android:scrollbars="vertical"/>

            <ProgressBar
            android:id="@+id/prog_load_more"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:visibility="gone"/>

            </LinearLayout>

        </FrameLayout>

</android.support.v4.widget.SwipeRefreshLayout>
like image 643
Faisal Tai Avatar asked Jan 30 '16 11:01

Faisal Tai


2 Answers

I have had the same issue before. It seems to happen when you don't have your RecyclerView as a direct child of your SwipeRefreshLayout.

We fixed this issue by using a custom SwipeRefreshLayout like this one:

public class FixedSwipeRefreshLayout extends SwipeRefreshLayout {

    private RecyclerView recyclerView;

    public FixedSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FixedSwipeRefreshLayout(Context context) {
        super(context);
    }

    public void setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }

    @Override
    public boolean canChildScrollUp() {
        if (recyclerView!=null) {
            return recyclerView.canScrollVertically(-1);
        }

        return false;
    }

}

Then, instead of using android.support.v4.widget.SwipeRefreshLayout use your own 'com.example.app.FixedSwipeRefreshLayout' like this:

<?xml version="1.0" encoding="utf-8"?>
<com.example.app.FixedSwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
...
</com.example.app.FixedSwipeRefreshLayout>

And finally, when you get a handle of your SwipeRefreshLayout, use your custom one and set your RecyclerView:

FixedSwipeRefreshLayout swipe_refresh;
swipe_refresh = (FixedSwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh);
RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.gv_feeds);
swipe_refresh.setRecyclerView(mRecyclerView);

Hope this helps.

like image 158
FMontano Avatar answered Sep 23 '22 17:09

FMontano


As per swipeRefreshDocumentation SwipeRefreshLayout does not play well with layout that are not scrollable if added inside it : you have 2 solution for this:

1)change your layout to make swipeRefreshLayout to have direct child as recyclerView .

something like this:

<?xml version="1.0" encoding="utf-8"?>

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:id="@+id/ll_HeaderProgress"
        android:background="@color/white_color_30">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <android.support.v4.widget.SwipeRefreshLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/swipe_refresh"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="9"
               >
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview_buzz"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"/>
</android.support.v4.widget.SwipeRefreshLayout>
            <ProgressBar
                android:id="@+id/ll_footerProgress"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_gravity="center_horizontal"
                android:visibility="gone"/>
        </LinearLayout>
    </FrameLayout>

2)Extend SwipeRefreshLayout with listener to be able to add various conditions from the classes that display this layout.

Something like the following: RefreshSwipeRefreshLayout.java

public class RefreshSwipeRefreshLayout extends SwipeRefreshLayout {   
  private OnChildScrollUpListener mScrollListenerNeeded;

  public interface OnChildScrollUpListener {
    boolean canChildScrollUp();
  }

  public RefreshSwipeRefreshLayout(Context context) {
    super(context);   
  }
  public RefreshSwipeRefreshLayout(Context context, AttributeSet attrs) {
    super(context, attrs);   
  }

 /**
  * Listener that controls if scrolling up is allowed to child views or not
  */
  public void setOnChildScrollUpListener(OnChildScrollUpListener listener) {
    mScrollListenerNeeded = listener;   
  }

  @Override
  public boolean canChildScrollUp() {
    if (mScrollListenerNeeded == null) {
      Log.e(GeneralSwipeRefreshLayout.class.getSimpleName(), "listener is not defined!");
    }
    return mScrollListenerNeeded != null && mScrollListenerNeeded.canChildScrollUp();
  }
}

and Then do this in class that displays SwipeRefreshLayout containing recyclerView layout, you can do something like this:

mSwipeLayout.setOnChildScrollUpListener(new OnChildScrollUpListener() {
  @Override
  public boolean canChildScrollUp() {
    return   ((LinearLayoutManager)mRecyclerView.getLayoutManager()).findFirstVisibleItemPosition().getFirstVisiblePosition() > 0 || 
           mRecyclerView.getChildAt(0) == null || 
           mRecyclerView.getChildAt(0).getTop() < 0;
  }

});

Also dont forget to change it in xml

like image 43
JAAD Avatar answered Sep 20 '22 17:09

JAAD