'When I am scrolling up SwipeRefreshLayout refreshing my app
I am created an app that downloaded some data from internet and show them in a list using recyclerView.So I added SwipeRefreshLayout so that when user is at the beginning of the page he can pull from top to refresh (like facebook app).But In my app when I scroll down and again trying to scrolling up the SwipeRefreshLayout shows up and refreshing my page.
I also search the internet but can't get the right answer.
I try this solution but it doesn't work anymore(Because I am using recyclerView).
Here is some of the code of my app for better understanding...
activity_main
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeToRefresh"
android:layout_height="match_parent"
android:layout_width="match_parent">
<include layout="@layout/content_main"/>
</android.support.v4.widget.SwipeRefreshLayout>
MainActivity.java
//.....
public SwipeRefreshLayout mSwipeRefreshLayout;
protected void onCreate(Bundle savedInstanceState) {
//....
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeToRefresh);
mSwipeRefreshLayout.setOnRefreshListener(this);
//......
}
//.......
@Override
public void onRefresh() {
Api.getBlog(mBlogListAdapter);
}
Api Response
//.......
@Override
public void onResponse(Call<AllBlog> call, Response<AllBlog> response) {
//.......
mActivity.mSwipeRefreshLayout.setRefreshing(false);
}
In my adapter
//........
public class BlogListViewHolder extends RecyclerView.ViewHolder implements View.OnScrollChangeListener{
public ImageView mBlogImage;
public TextView mBlogTitle;
public TextView mBlogAuthor;
public BlogListViewHolder(View itemView) {
super(itemView);
mBlogImage = (ImageView) itemView.findViewById(R.id.blogPhoto);
mBlogTitle = (TextView) itemView.findViewById(R.id.blogTitle);
mBlogAuthor = (TextView) itemView.findViewById(R.id.blogAuthor);
}
}
I also tried implementing View.OnScrollChangeListener but it also not work.
public class BlogListViewHolder extends RecyclerView.ViewHolder implements View.OnScrollChangeListener{
public ImageView mBlogImage;
public TextView mBlogTitle;
public TextView mBlogAuthor;
public BlogListViewHolder(View itemView) {
super(itemView);
mBlogImage = (ImageView) itemView.findViewById(R.id.blogPhoto);
mBlogTitle = (TextView) itemView.findViewById(R.id.blogTitle);
mBlogAuthor = (TextView) itemView.findViewById(R.id.blogAuthor);
itemView.setOnScrollChangeListener(this);
}
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (v.getVerticalScrollbarPosition() == 0) {
mActivity.mSwipeRefreshLayout.setEnabled(true);
} else {
mActivity.mSwipeRefreshLayout.setEnabled(false);
}
}
}
Solution 1:[1]
I think you have implemented SwipeRefreshLayout to whole layout itself. This is not the correct way to implement SwipeRefreshLayout. You should wrap SwipeRefreshLayout to just your RecyclerView, instead of whole layout.
Like below:
<android.support.v4.widget.SwipeRefreshLayout
...
>
<RecyclerView
...
/>
</android.support.v4.widget.SwipeRefreshLayout>
Solution 2:[2]
//create interface
public interface YourFragmentInterface {
void fragmentBecameVisible();
}
//implements YourFragmentInterface
@Override
public void fragmentBecameVisible()
// refresh detail
}
Solution 3:[3]
Use SwipeRefreshLayout this way.
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
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:background="#e0e0e0"
android:overScrollMode="never">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
Solution 4:[4]
For me I had to remove the following line which I had added to test something else,
recyclerView.setNestedScrollingEnabled(false);
And for the record, I have applied SwipeRefreshLayout to the whole layout itself, not wrapped it to just my RecyclerView. Dunno if it is right or wrong but it does work.
Hope this helps someone!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Chintan Soni |
Solution 2 | pamitha |
Solution 3 | Dnyaneshwar Panchal |
Solution 4 | Dhunju_likes_to_Learn |