我创建了一个从互联网上下载一些数据的应用程序,并使用recyclerView在列表中显示它们。所以我添加了SwipeRefreshLayout,这样当用户位于页面的开头时,他可以从顶部拉到刷新(如facebook app) 。但在我的应用程序中,当我向下滚动并再次尝试向上滚动时,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);
}
}
}
答案 0 :(得分:7)
我认为您已将SwipeRefreshLayout实现为整个布局本身。 这不是实现SwipeRefreshLayout的正确方法。您应该将SwipeRefreshLayout包装到您的RecyclerView,而不是整个布局。
如下所示:
<android.support.v4.widget.SwipeRefreshLayout
...
>
<RecyclerView
...
/>
</android.support.v4.widget.SwipeRefreshLayout>
答案 1 :(得分:1)
以这种方式使用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>
答案 2 :(得分:0)
//create interface
public interface YourFragmentInterface {
void fragmentBecameVisible();
}
//implements YourFragmentInterface
@Override
public void fragmentBecameVisible()
// refresh detail
}