简体   繁体   English

CoordinatorLayout + SwipeRefreshLayout + Endless RecyclerView的怪异行为

[英]Weird behavior with CoordinatorLayout+SwipeRefreshLayout+Endless RecyclerView

I'm trying to implement a CoordinatorLayout with a ToolBar that collapses. 我正在尝试使用折叠的ToolBar实现CoordinatorLayout I already have a SwipeRefreshLayout with a RecyclerView inside. 我已经有一个带有RecyclerViewSwipeRefreshLayout This recycler view also has an onScrollListener to load more content and a custom adapter to show a loading ViewHolder when it's loading more content. 此回收器视图还具有一个onScrollListener来加载更多内容,以及一个自定义适配器,以在加载更多内容时显示正在加载的ViewHolder

Everything was working fine before I tried adding the CoordinatorLayout . 在尝试添加CoordinatorLayout之前,一切工作正常。 Now I have two problems: 现在我有两个问题:

  1. When loading items for the first time, the loading ViewHolder shows up and it's well placed (below the ToolBar ). 第一次加载项目时,将显示加载的ViewHolder ,并且位置正确(在ToolBar下方)。 When it finishes, the loading is removed and the items are added. 完成后,将删除加载并添加项目。 The problem is that the first item is hidden. 问题在于第一项是隐藏的。 It's like the second item is actually the first one. 就像第二项实际上是第一项。 It's completely impossible to see the first item even when it is bigger than the ToolBar . 即使第一个项目大于ToolBar ,也完全不可能看到它。 But when I use swipe to refresh, the item gets placed properly. 但是,当我使用滑动刷新时,该项目会正确放置。 I have no idea why this happens. 我不知道为什么会这样。

  2. When I'm using swipe to refresh, it loads items two times. 当我使用滑动刷新时,它会加载两次项目。 The first time is the normal load and the second is a load for more items because of the onScrollListener . 第一次是正常加载,第二次是由于onScrollListener加载了更多项目。 However the scroll is still on the top of the list. 但是,滚动条仍位于列表的顶部。 The recycler view items stay invisible until I scroll (I think this is because I only notify the adapter of the new items but we are still at the top of the list). 在我滚动之前,回收者视图项保持不可见(我认为这是因为我仅将新项通知适配器,但我们仍位于列表的顶部)。

However, I don't know what to change in the listener to fix this. 但是,我不知道要在监听器中进行哪些更改以解决此问题。

Here's the listener: 这是听众:

    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);
                }
            }
        }
    });

My activity layout: 我的活动布局:

<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>

My toolbar: 我的工具栏:

<?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>

Both problems are fixed. 这两个问题都已解决。 For the first one, when I was removing the loading ViewHolder I was actually doing notifyItemInserted instead of notifyItemRemoved . 对于第一个,当我删除加载的ViewHolder时,我实际上是在做notifyItemInserted而不是notifyItemRemoved

For the second problem it's because I'm calling notifyDatasetChanged when I'm using swipe to refresh. 对于第二个问题,是因为我在使用滑动刷新时正在调用notifyDatasetChanged

@Override public void onRefresh() {
    adapter.clear();
    adapter.notifyDataSetChanged();
    loadSubmissions(true);
}

When I clear the adapter with the swipe to refresh, I'm swiping so it means that the onScrollListener will be called. 当我通过滑动刷擦清除适配器以刷新时,我正在刮擦,这意味着将调用onScrollListener。

I changed the listener and added a verification to don't load more if there are no items. 我更改了监听器,并添加了一个验证以确保在没有项目的情况下不会加载更多内容。

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.

相关问题 带有SwipeRefreshLayout + CoordinatorLayout和菜单的RecyclerView - RecyclerView with SwipeRefreshLayout + CoordinatorLayout with Menu RecyclerView和CoordinatorLayout.Behavior - RecyclerView and CoordinatorLayout.Behavior 使用CoordinatorLayout与SwipeRefreshLayout以及RecyclerView和FloatingActionButton一起使用 - Using CoordinatorLayout with SwipeRefreshLayout and RecyclerView and FloatingActionButton together CoordinatorLayout中的Recyclerview-无休止的滚动问题 - Recyclerview inside CoordinatorLayout - Endless scrolling issue 在coordinatorlayout的recyclerview上实现自定义行为 - implementing a custom behavior on recyclerview inside coordinatorlayout 如何控制Scroll StaggeredGrid RecyclerView上的SwipeRefreshLayout和Endless Fuction? - how to control SwipeRefreshLayout and Endless Fuction on Scroll StaggeredGrid RecyclerView? 自定义CoordinatorLayout.Behavior和RecyclerView滚动问题 - Custom CoordinatorLayout.Behavior and RecyclerView scroll issue RecyclerView中SortedList的怪异行为 - Weird behavior of SortedList in RecyclerView RecyclerView的怪异行为 - Weird Behavior of RecyclerView 协调器布局不使用 swipeRefreshlayout 滚动 - coordinatorlayout not scrolling with swipeRefreshlayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM