问题描述
我正在使用findLastCompletelyVisibleItemPosition()
确定RecyclerView中的最后一个可见项目.
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);
布局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.
但是在横向模式下,返回的位置始终为-1.
However in landscape mode, the position returned is always -1.
我的问题是:
有人知道为什么会这样吗?
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?
推荐答案
您看到的"-1"是RecyclerView.NO_POSITION
返回码,表明存在在横向模式下在RecyclerView中没有完全可见的位置. 完全可见"是指整个视图,包括所有装饰及其边距(垂直方向的上/下边距和左/右对于水平方向)是可见的.您可能会看到所有数据,但单个像素可能会偷偷溜走观点.看看findLastCompletelyVisibleItemPosition()
及其调用findOneVisibleChild()
此处.
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 entireview 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 outof view. Take a look at findLastCompletelyVisibleItemPosition()
and its invocation of findOneVisibleChild()
here.
再次检查您在RecyclerView中的可见位置是否具有100%的可见性,如上所述.在开发人员中显示利润模式等.如果您确认至少有一个完全可见的位置,则还有其他更神秘的事情发生.
Double check that you have a 100% visible position in your RecyclerView as outlined above. Show margins in developermode, etc. Something else more mysterious is going on if you verify that there is at least one completely visible position.
这篇关于RecyclerView LinearLayout管理器始终在横向模式下返回-1-findLastCompletelyVisibleItemPosition()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!