Recyclerview表示到达最后一项

问题描述 投票:0回答:3

我有一个recycleView,当我到达最后一个项目时我需要观察但是我注意到它总是表明我到达了最后一个项目,即使我还没有滚动。我的设置回收站的代码:

newsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
newsRecyclerView.setFocusable(false);
newsRecyclerView.setNestedScrollingEnabled(false);
newsAdapter = new NewsAdapter(getContext(), newsDetails, categoryNumber);
newsRecyclerView.setAdapter(newsAdapter);
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

我的xml代码是:

            <android.support.v7.widget.RecyclerView
            android:id="@+id/news_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="4dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/news_top_stories_title_text_view" />
android android-layout android-recyclerview
3个回答
1
投票

这是我放入我的Util中的代码,可以在任何地方使用。

Util.setRecyclerViewLastPositionListner(rv, linearLayoutManager , new UtilitiesV2.OnLastPositionReached() {
            @Override
            public void onReached() {
                // last position reached
            }
        });

把它放在Util中。

 private boolean userScrolled = true;
    int pastVisiblesItems, visibleItemCount, totalItemCount;


    public interface OnLastPositionReached {
        void onReached();
    }

    public void setRecyclerViewLastPositionListner(RecyclerView rvBooksMockTest, final LinearLayoutManager mLayoutManager, final OnLastPositionReached onLastPositionReached) {
        rvBooksMockTest.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                    userScrolled = true;
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                super.onScrolled(recyclerView, dx, dy);
                // Here get the child count, item count and visibleitems
                // from layout manager

                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                // Now check if userScrolled is true and also check if
                // the item is end then update recycler view and set
                // userScrolled to false
                if (userScrolled && (visibleItemCount + pastVisiblesItems) == totalItemCount) {
                    userScrolled = false;
                    if (onLastPositionReached != null) onLastPositionReached.onReached();
                }
            }
        });
    }

更新根据您的要求,这里是NestedScrollView底部到达监听器。

nestedScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
    @Override
    public void onScrollChanged() {
        if (nestedScrollView != null) {
            if (nestedScrollView.getChildAt(0).getBottom() <= (nestedScrollView.getHeight() + nestedScrollView.getScrollY())) {
                //scroll view is at bottom
            } else {
                //scroll view is not at bottom
            }
        }
    }
});

0
投票

将此代码添加到NewsAdapter类:

private List<News> mNewsList;

public NewsAdapter(Context context, List<NewDetails> newsList) {
    mNewsList = newsList;
}

private int size() {
   return mNewsList != null ? mNewsList.size() : 0;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
   if(position == size() - 1) {
       // TODO: Reach the last item in recycle view
   }
}

试一试!


0
投票

感谢Khemraj给出解决方案的提示,因为我在recyclerviewNestedScrollView中有一个coordinator layout作为他们的父母

我已经解决了这个问题:

public Disposable observeNestedScroll(LoadMoreListener listener) {
    return RxNestedScrollView.scrollChangeEvents(nestedScrollView)
            .subscribe(
                    viewScrollChangeEvent -> {
                        NestedScrollView nestedScrollView =(NestedScrollView) viewScrollChangeEvent.view();
                        if(nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1) != null) {
                            if ((viewScrollChangeEvent.scrollY() >= (nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1).getMeasuredHeight() - nestedScrollView.getMeasuredHeight())) &&
                                    viewScrollChangeEvent.scrollY() > viewScrollChangeEvent.oldScrollY()) {
                                listener.onLoadMore();
                            }
                        }
                    });
}
© www.soinside.com 2019 - 2024. All rights reserved.