【发布时间】:2016-03-29 10:19:01
【问题描述】:
我创建了一个应用程序,它从互联网下载了一些数据并使用 recyclerView 在列表中显示它们。所以我添加了 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);
}
}
}
【问题讨论】:
-
请将您的代码发布为
content_main.xml。
标签: android swiperefreshlayout