滑动刷新布局不适用于线性布局管理器
Swipe to refresh layout not working with linear layout manager
我有一个按预期工作的 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 的每一行,问题仍然存在,只有当我没有设置线性布局管理器时,滑动才能刷新。
通过 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);
经过更多调试后发现问题是 RecyclerView 元素,列表的第一个元素被隐藏(Visibility.gone)所以每次列表到达顶部列表时 linearLayoutManger 都试图获得第一个未显示的可见元素,因此从未调用刷新动画的滑动。
我有一个按预期工作的 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 的每一行,问题仍然存在,只有当我没有设置线性布局管理器时,滑动才能刷新。
通过 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);
经过更多调试后发现问题是 RecyclerView 元素,列表的第一个元素被隐藏(Visibility.gone)所以每次列表到达顶部列表时 linearLayoutManger 都试图获得第一个未显示的可见元素,因此从未调用刷新动画的滑动。