[英]RecyclerView with SwipeRefreshLayout + CoordinatorLayout with Menu
[英]Weird behavior with CoordinatorLayout+SwipeRefreshLayout+Endless RecyclerView
我正在尝试使用折叠的ToolBar
实现CoordinatorLayout
。 我已经有一个带有RecyclerView
的SwipeRefreshLayout
。 此回收器视图还具有一个onScrollListener
来加载更多内容,以及一个自定义适配器,以在加载更多内容时显示正在加载的ViewHolder
。
在尝试添加CoordinatorLayout
之前,一切工作正常。 现在我有两个问题:
第一次加载项目时,将显示加载的ViewHolder
,并且位置正确(在ToolBar
下方)。 完成后,将删除加载并添加项目。 问题在于第一项是隐藏的。 就像第二项实际上是第一项。 即使第一个项目大于ToolBar
,也完全不可能看到它。 但是,当我使用滑动刷新时,该项目会正确放置。 我不知道为什么会这样。
当我使用滑动刷新时,它会加载两次项目。 第一次是正常加载,第二次是由于onScrollListener
加载了更多项目。 但是,滚动条仍位于列表的顶部。 在我滚动之前,回收者视图项保持不可见(我认为这是因为我仅将新项通知适配器,但我们仍位于列表的顶部)。
但是,我不知道要在监听器中进行哪些更改以解决此问题。
这是听众:
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(!adapter.isLoading()) {
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
//position starts at 0
if (layoutManager.findLastCompletelyVisibleItemPosition() >= layoutManager.getItemCount() - 2) {
loadSubmissions(false);
}
}
}
});
我的活动布局:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/contentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="6dp"
android:paddingTop="6dp"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
我的工具栏:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="4dp"
app:layout_scrollFlags="scroll|enterAlways">
</android.support.v7.widget.Toolbar>
这两个问题都已解决。 对于第一个,当我删除加载的ViewHolder时,我实际上是在做notifyItemInserted
而不是notifyItemRemoved
。
对于第二个问题,是因为我在使用滑动刷新时正在调用notifyDatasetChanged
。
@Override public void onRefresh() {
adapter.clear();
adapter.notifyDataSetChanged();
loadSubmissions(true);
}
当我通过滑动刷擦清除适配器以刷新时,我正在刮擦,这意味着将调用onScrollListener。
我更改了监听器,并添加了一个验证以确保在没有项目的情况下不会加载更多内容。
mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int current_page) {
// Doesnt load multiples times with the same scroll and doesnt load if there are no items
if(!adapter.isLoading() && adapter.getItemCount() > 0) {
loadSubmissions(false);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.