【问题标题】:How to get current item position of recyclerview while scrolling a NestedScrollView?滚动 NestedScrollView 时如何获取 recyclerview 的当前项目位置?
【发布时间】:2019-05-05 16:57:29
【问题描述】:

我在nestedScrollView android中有recycler view,如何在滚动nestedScrollView时获取recyclerview的当前item位置

    <?xml version="1.0" encoding="utf-8"?>
      <android.support.v4.widget.NestedScrollView
       xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/constraintMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
       <Linearlayout 
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">
           <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:layout_marginBottom="5dp"
            android:src="@drawable/logo" />


            <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_view_all"
                    android:layout_width="match_parent"
                    android:nestedScrollingEnabled="false"
                    android:layout_height="wrap_content"
                    android:scrollbars="vertical" />

        </Linearlayout>

  </android.support.v4.widget.NestedScrollView>

addOnScrollListenersetOnScrollListener 函数 不使用 recyclerview 调用

【问题讨论】:

    标签: android android-layout android-recyclerview android-nestedscrollview


    【解决方案1】:

    您可以从布局管理器中找到回收站视图的第一个可见项。

    LinearLayoutManager myLayoutManager = myRecyclerView.getLayoutManager();
    int scrollPosition = myLayoutManager.findFirstVisibleItemPosition();
    

    其他都是

    findFirstCompletelyVisibleItemPosition()
    findLastCompletelyVisibleItemPosition()
    

    根据需要使用。

    【讨论】:

    • 它总是返回 0
    • 当前项目是什么意思?您在单击时选择的项目或列表中顶部可见的项目?如果您想要顶部可见的项目,那么上面的代码将适合您。如果您想获取从列表中选择的项目。为此,您必须为回收站视图适配器中的每个项目监听一个点击事件。
    • 我只是在 onScrollChange 中将这段代码用于nestedscrollview,但它的 stell 显示 scrollPosition 为 0
    • 请告诉我,您所说的当前职位是什么意思?它是在顶部还是点击的项目?
    猜你喜欢
    • 1970-01-01
    • 2017-07-28
    • 2020-05-17
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多