RecyclerView LinearLayout manager always returns -1 in landscape mode - findLastCompletelyVisibleItemPosition ()

I use findLastCompletelyVisibleItemPosition() to determine the last visible item in my RecyclerView.

Here is a code snippet of how I customize my layout:

  mRecyclerView.setHasFixedSize(true); LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext()); mRecyclerView.setLayoutManager(mLayoutManager); 

XML format:

 <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe_container" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/message_list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:paddingBottom="@dimen/footer_progress_bar" android:paddingTop="16dp" android:scrollbars="vertical" /> <ProgressBar android:id="@+id/progress_bar" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center" android:visibility="gone" /> <ProgressBar android:id="@+id/footer_progress_bar" android:layout_width="@dimen/footer_progress_bar" android:layout_height="@dimen/footer_progress_bar" android:layout_gravity="center|bottom" android:visibility="gone" /> </FrameLayout> </android.support.v4.widget.SwipeRefreshLayout> 

In portrait mode, this works great and always returns to the correct position.

However, in landscape mode, the returned position is always -1.

My question is:

Does anyone know why this is happening?

How can I override this to get the right position back?

Or can someone recommend another solution to get the last position in the landscape right?

+6
source share
3 answers

The β€œ-1” that you see is the RecyclerView.NO_POSITION return code indicating that it is not a fully visible position in your RecyclerView in landscape mode. β€œFully visible” means all including any decorations and their margins (upper and lower margins for vertical orientation and left / right for horizontal orientations). You can see all of your data, but one pixel may disappear view. Take a look at findLastCompletelyVisibleItemPosition() and its call to findOneVisibleChild() here .

Double check that you have a 100% visible position in your RecyclerView, as described above. Show margin in developer mode, etc. Something even more mysterious happens if you make sure that there is at least one completely visible position.

+6
source

This is because there is no visible element completely , it is caused by the fact that either there are no objects that are displayed at all, or your objects are larger than your screen and, therefore, are not completely .

You should use the findLastVisibleItemPosition() method to get the last visible item on the screen, as this will return the position of the last item that will be visible on the screen, even if it contains only one pixel of that item. I do not know if this will meet your requirements, if not, you can create a new question with a lot of context / details about your requirements.

+3
source

You tried to initialize it to if (savedInstanceState == null)

 if (savedInstanceState == null){ mRecyclerView.setHasFixedSize(true); LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext()); mRecyclerView.setLayoutManager(mLayoutManager); } else { // Do nothing } 
0
source

All Articles