Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Recyclerview表示到达最后一项_Android_Android Layout_Android Recyclerview - Fatal编程技术网

Android Recyclerview表示到达最后一项

Android Recyclerview表示到达最后一项,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,我有一个recycleView,当到达最后一个项目时,我需要观察它,但我注意到它总是指示我到达了最后一个项目,即使我还没有滚动。 我的设置回收商代码: newsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); newsRecyclerView.setFocusable(false); newsRecyclerView.setNestedScrollingEnabled(false); newsAdapter

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

这是我放在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
            }
        }
    }
});
nestedScrollView.getViewTreeObserver().addOnScrollChangedListener(新的ViewTreeObserver.OnScrollChangedListener(){
@凌驾
public void onScrollChanged(){
如果(nestedScrollView!=null){
if(nestedScrollView.getChildAt(0.getBottom()感谢为解决方案提供提示的人
因为我在
NestedScrollView
中有一个
recyclerview
协调器布局
作为它们的父级

我已经这样解决了我的问题:

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

我已经看到很多人对这个问题的回答,我认为他们都没有给出准确的行为作为结果。但是如果你遵循这个方法,我肯定你会得到最好的行为

rvCategories是您的回收视图

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

您需要在recyclerview上使用
onScrollListener
,然后查找最后一个
((LinearLayoutManager)vYourRecycler.getLayoutManager())。FindLastCompleteyVisibleItemPosition()我尝试了它,但它被调用一次当视图创建可能是你的RealReVIEW不可滚动。或者它有1-2个项目。我有一个<代码> ScVIEW视图<代码>,它包含一个“约束布局”,它包含了<代码> CurryReVIEW < /Cord>。所以我应该考虑观察ScLoVIEW。尝试用NeStdScVIEW视图代替ScRelVIEW。您为我提供了一个链接,用于检测
nestedScrollView
何时到达末尾