jjzjj

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

coder 2023-11-30 原文

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

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

    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 中没有完全可见的位置。 “完全可见”是指整个 View 包括任何装饰及其边距(垂直方向的上/下边距和左/右 对于水平方向)是可见的。您可能会看到所有数据,但可能会漏掉一个像素 观点。看看 findLastCompletelyVisibleItemPosition() 及其 调用 findOneVisibleChild() here .

仔细检查您的 RecyclerView 中是否有一个 100% 可见的位置,如上所述。在开发人员中显示边距 模式等。如果您确认至少有一个完全可见的位置,就会发生其他更神秘的事情。

关于android - RecyclerView LinearLayout 管理器在横向模式下总是返回 -1 - findLastCompletelyVisibleItemPosition(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37113812/

有关android - RecyclerView LinearLayout 管理器在横向模式下总是返回 -1 - findLastCompletelyVisibleItemPosition()的更多相关文章

随机推荐