【问题标题】:How to reset recyclerview itemlist to start position如何将recyclerview项目列表重置为起始位置
【发布时间】:2019-07-02 04:59:47
【问题描述】:

我有 3 个按钮和一个 Recyclerview。单击每个按钮后,我将更新Recyclerview 列表。我正在使用以下方式更新 Recyclerview 列表:

fun updateList(list: List<SortFilterItem>){
    itemList.clear()
    itemList.addAll(list)
    notifyDataSetChanged()
  }

问题是如果我将列表滚动到某个位置,然后单击任何Button,列表会更新,但位置不会重置为起始位置。更新列表时如何重置Recyclerview 的位置。

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:
    LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
    layoutManager.scrollToPositionWithOffset(0, 0); //Way I
    layoutManager.smoothScrollToPosition(mRecyclerView, null, 0); //Way II
    
    【解决方案2】:

    试试yourLayourManager.scrollToPositionWithOffset(0,0),这将滚动直到满足偏移量。

    recyclerView.smoothScrollToPosition(position)

    【讨论】:

      【解决方案3】:

      更新代码时只需在按钮单击中添加一行

      recyclerView.scrollToPosition(0);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-13
        • 2019-01-01
        • 1970-01-01
        • 2016-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多