【问题标题】:RecyclerView inside NestedScrollView smoothScrollNestedScrollView 内的 RecyclerView smoothScroll
【发布时间】:2018-02-19 17:52:15
【问题描述】:

我在 NestedScrollView 中有一个 recyclerView。我知道这不是一个好习惯,但我需要一个监听器来监听滚动。

当用户到达recyclerView的最后时,监听器包含在notify上。这有一个带有 3 个网格的 gridLayoutManager,可见的行数取决于屏幕的大小。

除了smoothScroll,一切正常。

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nestedGalleryAll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/galleryAll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.v4.widget.NestedScrollView>

这就是我的应用程序的工作方式。

【问题讨论】:

  • 你到底想要什么??平滑滚动??
  • 是的,只有smoothScroll不会丢失nestedScrollView的监听器
  • 我已经更新了我的答案..
  • 顺便说一句,为什么你需要嵌套滚动视图的监听器??

标签: android android-recyclerview smooth-scrolling android-nestedscrollview nestedscrollview


【解决方案1】:

试试这个.. recylerView.setNestedScrollingEnabled(false); 在你的活动中...... 编辑通知用户,如果他到达结束..使用这个

recyclerView.addOnScrollListener(new      RecyclerView.OnScrollListener() { 

   @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy)
   {  

  LinearLayoutManager layoutManager=LinearLayoutManager.class.cast(recyclerView.getLayoutManager()); 
   int totalItemCount = layoutManager.getItemCount();

   int lastVisible = 
    layoutManager.
    findLastVisibleItemPosition();                     boolean
 endHasBeenReached 
 = lastVisible + 5 
    >= totalItemCount;
 if (totalItemCount > 0 && endHasBeenReached) 

 { //you have reached to the bottom of your recycler view } } });

【讨论】:

  • 如果我这样做,我的听众将被禁用。当用户结束时,我需要那个监听器来更新 recyclerView 的项目。
  • 我已经更新了代码..将滚动监听器添加到 recyclerview..当最后一个项目可见时 notify user 或获取下一个项目..
  • 测试一下,如果您还有其他问题,请告诉我
  • 成功了!谢谢。我正在使用 Kotlin,我在翻译 LinearLayoutManager.class.cast(recyclerView.getLayoutManager()) 时遇到问题,但我翻译为 LinearLayoutManager::class.java.cast(recyclerView.layoutManager) 并工作。多亏了这一点,我不再使用 NestedScrollView。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-15
  • 2017-05-28
  • 2019-03-21
  • 1970-01-01
  • 2019-04-26
  • 2019-06-14
  • 2020-06-01
相关资源
最近更新 更多