【问题标题】:When I am scrolling up SwipeRefreshLayout refreshing my app当我向上滚动 SwipeRefreshLayout 刷新我的应用程序
【发布时间】: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


【解决方案1】:

我认为您已经将 SwipeRefreshLayout 实现到整个布局本身。 这不是实现 SwipeRefreshLayout 的正确方法。您应该将 SwipeRefreshLayout 包装到您的 RecyclerView,而不是整个布局。

如下:

<android.support.v4.widget.SwipeRefreshLayout
...
>
    <RecyclerView 
     ...
    />
</android.support.v4.widget.SwipeRefreshLayout>

【讨论】:

  • 谢谢伙计。我只是把我的整个布局放入 SwipeRefreshLayout。当使用 SwipeRefreshLayout 只包装 recyclerview 时问题解决了
  • @ChintanSoni 请也回答这个问题:stackoverflow.com/questions/36434071/…
【解决方案2】:
 //create interface
  public interface YourFragmentInterface {
   void fragmentBecameVisible();
  }

 //implements YourFragmentInterface

        @Override
        public void fragmentBecameVisible() 

        // refresh detail
        }

【讨论】:

    【解决方案3】:

    以这种方式使用 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>
    

    【讨论】:

      猜你喜欢
      • 2020-07-06
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      相关资源
      最近更新 更多