Java RecyclerView findLastCompletelyVisibleItemPosition始终返回最后一个元素

Java RecyclerView findLastCompletelyVisibleItemPosition始终返回最后一个元素,java,android,android-recyclerview,Java,Android,Android Recyclerview,关于StackOverflow的第一个问题 我有一个无限滚动的回收视图,前面有几个其他视图,都在嵌套的滚动视图中 每当我在RecyclerView的布局管理器上调用findLastCompletelyVisibleItemPosition时,我都会得到在RecyclerView中加载的最后一个元素的位置…即使只有第一个元素在屏幕上可见。我相信这是因为RecyclerView在NestedScrollView中--项目正在显示,但是我必须向下滚动才能查看它--但我肯定是错的 在片段OnViewCr

关于StackOverflow的第一个问题

我有一个无限滚动的回收视图,前面有几个其他视图,都在嵌套的滚动视图中

每当我在RecyclerView的布局管理器上调用findLastCompletelyVisibleItemPosition时,我都会得到在RecyclerView中加载的最后一个元素的位置…即使只有第一个元素在屏幕上可见。我相信这是因为RecyclerView在NestedScrollView中--项目正在显示,但是我必须向下滚动才能查看它--但我肯定是错的

在片段OnViewCreated中初始化RecyclerView:

@覆盖
已创建公用void onview(@NonNull视图,Bundle savedInstanceState){
super.onViewCreated(视图,savedInstanceState);
//…无关代码//
PhotoRecyclerView=view.findViewById(R.id.profile\u recyclerview\u photos);
//将LayoutManager添加到RecyclerView。
GridLayoutManager=新的GridLayoutManager(getContext(),2);
PhotoRecyclerView.setLayoutManager(管理器);
//从NestedScrollView平滑滚动。
PhotoRecyclerView.setNestedScrollingEnabled(假);
//将适配器添加到RecyclerView。
ProfileRecycleServiceAdapter=新的ProfileRecycleServiceAdapter(getContext(),photoList);
setAdapter(ProfileRecycleServiceAdapter);
连接到RecyclerView的ScrollListener:


私有void initScrollListener(){
PhotoRecycleView.addOnScrollListener(新的RecycleView.OnScrollListener(){
@凌驾
CrollStateChanged上的公共void(@NonNull RecyclerView RecyclerView,int newState){
super.onScrollStateChanged(recyclerView、newState);
}
@凌驾
已克隆的公共void(@NonNull RecyclerView RecyclerView,int-dx,int-dy){
super.onScrolled(recyclerView、dx、dy);
final GridLayoutManager GridLayoutManager=(GridLayoutManager)recyclerView.getLayoutManager();
Log.d(标记“”+gridLayoutManager.findLastCompletelyVisibleItemPosition());
如果(!加载照片){
if(gridLayoutManager.findLastCompletelyVisibleItemPosition()==photoList.size()-1&&hasMorePhotos){
Log.d(标记“尝试加载更多图像”);
loadMore();
加载照片=真;
}
}
}
});
}
片段的布局:

<androidx.core.widget.NestedScrollView android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    tools:context=".ProfileFragment">


  <------- RANDOM OTHER VIEWS -------->

   <androidx.recyclerview.widget.RecyclerView
      android:id="@+id/profile_recyclerview_photos"
      android:layout_marginTop="10dp"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      app:layout_constraintStart_toStartOf="@+id/profile_guideline_verticalstart"
      app:layout_constraintEnd_toEndOf="@+id/profile_guideline_verticalend"
      app:layout_constraintTop_toBottomOf="@id/profile_textview_photoslabel"/>

  </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>



非常感谢您的帮助!!

经过一些研究——在来自的大量帮助下,我意识到这种行为是由于将RecyclerView高度设置为“包装内容”。这会导致它立即加载所有项目,因此最后一个“完全可见”项是RecyclerView中的最后一项。

您可以尝试使用约束布局并将RecyclerView高度更改为0dp


它会解决您的问题

您是如何解决的?您是如何解决的?