当我向上滚动 SwipeRefreshLayout 刷新我的应用程序时
When I am scrolling up SwipeRefreshLayout refreshing my app
我创建了一个应用程序,它从互联网上下载了一些数据并使用 recyclerView.So 在列表中显示它们我添加了 SwipeRefreshLayout 这样当用户在页面的开头时他可以从顶部拉出以刷新(像 facebook 应用程序)。但在我的应用程序中,当我向下滚动并再次尝试向上滚动时,SwipeRefreshLayout 出现并刷新我的页面。
我也在网上搜索,但找不到正确答案。
我尝试了 this 解决方案,但它不再有效(因为我正在使用 recyclerView)。
下面是我的应用程序的一些代码,以便更好地理解...
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 响应
//.......
@Override
public void onResponse(Call<AllBlog> call, Response<AllBlog> response) {
//.......
mActivity.mSwipeRefreshLayout.setRefreshing(false);
}
在我的适配器中
//........
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);
}
}
我也试过实施 View.OnScrollChangeListener 但它也不起作用。
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);
}
}
}
//create interface
public interface YourFragmentInterface {
void fragmentBecameVisible();
}
//implements YourFragmentInterface
@Override
public void fragmentBecameVisible()
// refresh detail
}
我认为您已经对整个布局本身实施了 SwipeRefreshLayout。
这不是实现 SwipeRefreshLayout 的正确方法。您应该将 SwipeRefreshLayout 包装到您的 RecyclerView,而不是整个布局。
如下所示:
<android.support.v4.widget.SwipeRefreshLayout
...
>
<RecyclerView
...
/>
</android.support.v4.widget.SwipeRefreshLayout>
这样使用 SwipeRefreshLayout。
<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>
对我来说,我不得不删除我为测试其他内容而添加的以下行,
recyclerView.setNestedScrollingEnabled(false);
郑重声明,我已将 SwipeRefreshLayout 应用于整个布局本身,而不是仅将其包装到我的 RecyclerView。不知道是对是错,但确实有效。
希望这对某人有所帮助!
我创建了一个应用程序,它从互联网上下载了一些数据并使用 recyclerView.So 在列表中显示它们我添加了 SwipeRefreshLayout 这样当用户在页面的开头时他可以从顶部拉出以刷新(像 facebook 应用程序)。但在我的应用程序中,当我向下滚动并再次尝试向上滚动时,SwipeRefreshLayout 出现并刷新我的页面。
我也在网上搜索,但找不到正确答案。
我尝试了 this 解决方案,但它不再有效(因为我正在使用 recyclerView)。
下面是我的应用程序的一些代码,以便更好地理解...
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 响应
//.......
@Override
public void onResponse(Call<AllBlog> call, Response<AllBlog> response) {
//.......
mActivity.mSwipeRefreshLayout.setRefreshing(false);
}
在我的适配器中
//........
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);
}
}
我也试过实施 View.OnScrollChangeListener 但它也不起作用。
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);
}
}
}
//create interface
public interface YourFragmentInterface {
void fragmentBecameVisible();
}
//implements YourFragmentInterface
@Override
public void fragmentBecameVisible()
// refresh detail
}
我认为您已经对整个布局本身实施了 SwipeRefreshLayout。 这不是实现 SwipeRefreshLayout 的正确方法。您应该将 SwipeRefreshLayout 包装到您的 RecyclerView,而不是整个布局。
如下所示:
<android.support.v4.widget.SwipeRefreshLayout
...
>
<RecyclerView
...
/>
</android.support.v4.widget.SwipeRefreshLayout>
这样使用 SwipeRefreshLayout。
<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>
对我来说,我不得不删除我为测试其他内容而添加的以下行,
recyclerView.setNestedScrollingEnabled(false);
郑重声明,我已将 SwipeRefreshLayout 应用于整个布局本身,而不是仅将其包装到我的 RecyclerView。不知道是对是错,但确实有效。
希望这对某人有所帮助!