检测高于屏幕高度的Android RecyclerView是否已达到结束

时间:2017-02-05 16:41:22

标签: android android-recyclerview infinite-scroll

当我的RecyclerView到达最后一个项目时,我尝试加载更多项目,并且所有内容都适用于适合屏幕的RecyclerViews,但是当它大于屏幕尺寸时(其他一些视图已打开)事情发生在它的上方或下方或其他东西出了问题,它总是将最后一项作为最后findLastVisibleItemPosition()个想法返回?

此代码适用于与屏幕高度匹配的recylerviews:

container = (RecyclerView)layout.findViewById(R.id.container);
    //container.addItemDecoration(new SpacesItemDecoration(10));
    final LinearLayoutManager llm = new LinearLayoutManager(base);
    container.setLayoutManager(llm);
    container.setHasFixedSize(true);
    container.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if( loading || allLoaded || llm.findLastVisibleItemPosition() < recyclerView.getAdapter().getItemCount()-1)
                return;
            bottomLoadingView.setVisibility(View.VISIBLE);
            offset += limit;
            new PostLoader(base).execute("");
        }
    });

但是当reclycerview与屏幕高度不匹配时它不会起作用,例如使用此布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">

            <behzad.bases.views.MyViewPager
                android:id="@+id/my_slider"
                android:layout_width="match_parent"
                android:layout_height="200dp"/>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <?xml version="1.0" encoding="utf-8"?>
                <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    xmlns:app="http://schemas.android.com/apk/res-auto">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/container"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:fitsSystemWindows="true"/>

                    <include
                        layout="@layout/loading_view"/>

                    <include
                        layout="@layout/loading_bottom_view"/>

                </FrameLayout>   

            </LinearLayout>

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

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

</android.support.design.widget.CoordinatorLayout>

简化版xml:

<CoordinatorLayout>
<AppBarLayout & Collpasing>
    <!-- some content with fixed height-->
</AppBarLayout & Collpasing>
<container>
    <reclyerview>
</container>
</CoordinatorLayout>

问题在于,通过此布局,我总是收到llm.findLastVisibleItemPosition()中的最后一项,这使我的if毫无意义并且不断调用LoadMore直到{{ 1}}是真的。

我该如何解决这个问题?在这种情况下,我想我应该用其他内容替换allLoaded,但不知道是什么。

0 个答案:

没有答案