Recyclerview指示到达最后一个项目

时间:2018-08-12 23:05:46

标签: android android-layout android-recyclerview

我有一个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" />

4 个答案:

答案 0 :(得分:1)

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

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

将其放入实用程序中。

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

答案 1 :(得分: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
   }
}

尝试一下!

答案 2 :(得分:0)

感谢Khemraj给出解决方案的提示 因为我在recyclerview中有一个NestedScrollView,并且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();
                            }
                        }
                    });
}

答案 3 :(得分:0)

我已经看到很多关于这个问题的回答,我认为所有回答都没有给出准确的行为结果。但是,如果您遵循这种方法,我相信您会获得最佳行为。

rvCategories 是您的 RecyclerView

categoriesList 是传递给您的适配器的列表

binding.rvCategories.addOnScrollListener(object : RecyclerView.OnScrollListener() {
      override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                val position = (recyclerView.layoutManager as LinearLayoutManager).findLastCompletelyVisibleItemPosition()
                if (position + 1 == categoriesList.size) {
                   // END OF RECYCLERVIEW IS REACHED
                } else {
                    // END OF RECYCLERVIEW IS NOT REACHED
                }
        }
    })