问题
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);
}
}
}
回答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>
回答2:
//create interface
public interface YourFragmentInterface {
void fragmentBecameVisible();
}
//implements YourFragmentInterface
@Override
public void fragmentBecameVisible()
// refresh detail
}
回答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>
来源:https://stackoverflow.com/questions/36281497/when-i-am-scrolling-up-swiperefreshlayout-refreshing-my-app