android – RecyclerView LinearLayout管理器总是在横向模式下返回-1 – findLastCompletelyVisibleItemPosition()

我正在使用findLastCompletelyVisibleItemPosition()来确定我的RecyclerView中的最后一个可见项.

这是我如何设置布局的代码片段:

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>

在纵向模式下,此工作正常,并始终返回正确的位置.

但是在横向模式下,返回的位置始终为-1.

我的问题是:

有谁知道为什么会这样?

我怎样才能覆盖它以返回正确的位置?

或者任何人都可以推荐另一种解决方案来获得最后一个项目在景观中的正确位置?

解决方法

您看到的“-1”是RecyclerView.NO_POSITION返回代码,表示存在
在横向模式下,您的RecyclerView中没有完全可见的位置. “完全可见”意味着整个
视图包括任何装饰及其边距(垂直方向的顶部/底部边距和左/右)
对于水平方向)是可见的.您可能会看到所有数据,但单个像素可能会被偷偷溜走
观点.看看findLastCompletelyVisibleItemPosition()及其
调用findOneVisibleChild() here.

如上所述,仔细检查您在RecyclerView中是否有100%可见位置.在开发者中显示边距如果你确认至少有一个完全可见的位置,那么还会发生一些更神秘的事情.

以上是来客网为你收集整理的android – RecyclerView LinearLayout管理器总是在横向模式下返回-1 – findLastCompletelyVisibleItemPosition()全部内容,希望文章能够帮你解决android – RecyclerView LinearLayout管理器总是在横向模式下返回-1 – findLastCompletelyVisibleItemPosition()所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。