Sha*_*zan 4 java android android-recyclerview
StackOverflow 上的第一个问题!
我有一个无限滚动的RecyclerView,前面有几个其他视图,全部位于 NestedScrollView 内。
每当我调用findLastCompletelyVisibleItemPositionRecyclerView 的布局管理器时,我都会得到 RecyclerView 中加载的最后一个元素的位置......即使只有第一个元素在屏幕上可见。我相信这是因为 RecyclerView 位于 NestedScrollView 中——该项目正在显示,但是我必须向下滚动才能查看它——但我肯定是错的。
Fragment OnViewCreated 中 RecyclerView 的初始化:
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//... irrelevant code ... //
photosRecyclerView = view.findViewById(R.id.profile_recyclerview_photos);
// Add LayoutManager to RecyclerView.
GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
photosRecyclerView.setLayoutManager(manager);
// Smoothes scrolling from NestedScrollView.
photosRecyclerView.setNestedScrollingEnabled(false);
// Add adapter to RecyclerView.
profileRecyclerViewAdapter = new ProfileRecyclerViewAdapter(getContext(), photoList);
photosRecyclerView.setAdapter(profileRecyclerViewAdapter);
Run Code Online (Sandbox Code Playgroud)
ScrollListener 附加到 RecyclerView :
private void initScrollListener() {
photosRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
final GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
Log.d(TAG, "" + gridLayoutManager.findLastCompletelyVisibleItemPosition());
if (!loadingPhotos) {
if (gridLayoutManager.findLastCompletelyVisibleItemPosition() == photoList.size()-1 && hasMorePhotos) {
Log.d(TAG, "Attempting to load more images.");
loadMore();
loadingPhotos = true;
}
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
片段的布局:
<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>
Run Code Online (Sandbox Code Playgroud)
我们将非常感谢您的帮助!
经过一些研究——在这个答案的大量帮助下,我意识到这种行为是由于 RecyclerView 高度设置为换行内容所致。这会导致它立即加载所有项目,因此最后一个“完全可见”的项目是 RecyclerView 中的最后一个项目。
| 归档时间: |
|
| 查看次数: |
1984 次 |
| 最近记录: |