NestedScrollView+RecyclerView 滑動卡頓簡單解決方案

weixin_34320159發表於2017-10-11

今天遇到一個問題,就是那個使用NestedScrollView+RecyclerView會卡頓,這樣滑動起來會卡頓,給使用者帶來了很不好的體驗,也是想著去解決一下。於是我上了百度,哈哈,別我找到了解決方案,我記錄一下,也供大家參考一下。
在給RecyclerView設定layoutManager 的時候設定幾個引數,我附上原始碼的解釋,引數的意思我也大概的翻譯一下,意思差不多,方便大家理解

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    /**
     * When smooth scrollbar is enabled, the position and size of the scrollbar thumb is computed
     * based on the number of visible pixels in the visible items. This however assumes that all
     * list items have similar or equal widths or heights (depending on list orientation).
     * If you use a list in which items have different dimensions, the scrollbar will change
     * appearance as the user scrolls through the list. To avoid this issue,  you need to disable
     * this property.
     */
        //啟用平滑滾動條
        layoutManager.setSmoothScrollbarEnabled(true);

        /**Defines whether the layout should be measured by the RecyclerView or the LayoutManager
         * wants to handle the layout measurements itself.
          */
        //自動測量
        layoutManager.setAutoMeasureEnabled(true);

        recyclerView.setLayoutManager(layoutManager);
      
        //@param hasFixedSize true if adapter changes cannot affect the size of the RecyclerView.
        recyclerView.setHasFixedSize(true);
        //巢狀設定為不可滑動
        recycerView.setNestedScrollingEnabled(false);

ok,到此就結束了

相關文章