【问题标题】:Swipe to refresh layout not working with linear layout manager滑动以刷新布局不适用于线性布局管理器
【发布时间】:2017-11-28 13:03:09
【问题描述】:

我有一个按预期工作的 RecyclerView,我尝试添加一个滑动来刷新布局,如下所示:

    <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/srl_contacts"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/senderContainer"
    android:layout_below="@id/tv_connection_status">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_chat_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:padding="10dp"
        android:paddingBottom="3dp"
        android:paddingTop="3dp"
        android:scrollbars="none"></android.support.v7.widget.RecyclerView>

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

RecyclerView 工作正常,但没有刷卡刷新,然后我评论了我将线性布局管理器设置为 RecyclerView 和刷卡刷新实现的行,并调用了OnRefresh() 接口

在java中:

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRvList.setLayoutManager(layoutManager);  // <----- i commented this , and every thing worked fine , there was no data shown of course. 

可能是适配器的问题?我删除了我为 RecyclerView 设置适配器的每一行,问题仍然存在,只有当我没有设置线性布局管理器时,滑动才能刷新。

【问题讨论】:

    标签: android android-layout android-recyclerview material-design swiperefreshlayout


    【解决方案1】:

    经过更多调试后发现问题出在 RecyclerView 元素上,列表的第一个元素被隐藏(Visibility.gone),因此每次列表到达顶部列表时,linearLayoutManger 都试图获取第一个可见元素没有显示,因此从未调用过滑动刷新动画。

    【讨论】:

      【解决方案2】:

      RecyclerView 属性"layoutManager" 在 XML 中设置布局管理器时使用的构造函数。

      由于您没有在 xml 中使用它,我建议删除构造函数也删除 LinearLayoutManager 因为它是必需的构造函数,并替换为 StaggeredGridLayoutManager 而不传递构造函数,例如:

      StaggeredGridLayoutManager sglm =
                      new StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL);
              mRvList.setLayoutManager(sglm);
      

      或您可以在您的 recycleView XML 中设置 layououtManger,因为您的案例 layoutManager 是线性的:

      <android.support.v7.widget.RecyclerView
          android:id="@+id/rv_chat_list"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:clipToPadding="false"
          android:padding="10dp"
          android:paddingBottom="3dp"
          android:paddingTop="3dp"
          app:layoutManager="android.support.v7.widget.LinearLayoutManager"
          android:scrollbars="none"></android.support.v7.widget.RecyclerView>
      

      现在你可以像以前一样传递构造函数了:

      LinearLayoutManager layoutManager = new LinearLayoutManager(this);
      

      【讨论】:

      • 尝试了两种方式,但未能产生滑动刷新,但当然可以显示数据。
      • @Ahmedna 你应该先mSwipeRefreshLayout.setRefreshing(true);
      • 有点突破,滑动刷新动画现在总是显示,但是当实际滑动刷新发生时它不会被调用,这种行为对我来说很奇怪,从来没有遇到过滑动刷新布局
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多