Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
        }
    }
}
like image 537
Niyamat Ullah Avatar asked Mar 29 '16 10:03

Niyamat Ullah


1 Answers

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>
like image 106
Chintan Soni Avatar answered Sep 24 '22 02:09

Chintan Soni