6

I'm using findLastCompletelyVisibleItemPosition() to determine the last visible item in my RecyclerView.

Here is a code snippet of how I'm setting up my layout:

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

Layout XML:

<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 fine and always returns the right position.

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

My question is:

Does anyone know why this happens?

How I can override this to return the right position?

Or can anyone recommend another solution to get the right position of the last item in landscape?

AndroidEnthusiast
  • 6,557
  • 10
  • 42
  • 56

4 Answers4

9

The "-1" that you are seeing is the RecyclerView.NO_POSITION return code indicating that there is no completely visible position in your RecyclerView in landscape mode. "Completely visible" means that the entire view including any decorations and its margins (top/bottom margins for vertical orientation and left/right for horizontal orientations) is visible. You may see all of your data, but a single pixel could be sneaking off out of view. Take a look at findLastCompletelyVisibleItemPosition() and its invocation of findOneVisibleChild() here.

Double check that you have a 100% visible position in your RecyclerView as outlined above. Show margins in developer mode, etc. Something else more mysterious is going on if you verify that there is at least one completely visible position.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
4

This is happening because there is no item completely visible, this is caused because either there are no items shown at all, or your items are bigger than your screen and therefore are not completely visible.

You should consider using the findLastVisibleItemPosition() method to get the last visible item on the screen, altho this will return the position of the last item that is visible on the screen, even if it's only one pixel of that item. I don't know if this will forfill your requirements, if not, you might wanna create a new question with more context/details about your requirements.

Jeffalee
  • 1,080
  • 1
  • 7
  • 15
0

Have you tried initializing it in if (savedInstanceState == null)

if (savedInstanceState == null){
    mRecyclerView.setHasFixedSize(true);
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
} else {

    // Do nothing   
}
Atiq
  • 14,435
  • 6
  • 54
  • 69
  • Hmmm, not sure why I would need to do this. Can you expand a bit? – AndroidEnthusiast May 09 '16 at 11:12
  • when you rotate the screen activity gets recreated and the changes gets destroyed and if you do that as my example it wont recreate the `RecyclerView`and will keep the items positions saved as it was in portrait. – Atiq May 09 '16 at 11:26
  • okay i understand what you mean, I'll try that out thanks. But on the other hand. If a user starts my app in landscape mode, the same problem will occur. – AndroidEnthusiast May 09 '16 at 12:23
0

Before setAdapter (Listview/Recyclerview) you use below line its work.

recyclerviewList.setHasFixedSize(true);  
recyclerviewList.setNestedScrollingEnabled(false);  
LinearLayoutManager llm = new LinearLayoutManager(this);      
llm.setOrientation(LinearLayoutManager.VERTICAL);  
recyclerviewList.setLayoutManager(llm);  
recyclerviewList.setAdapter( adapter );
DGAD
  • 35
  • 5