Android NestedScrollView嵌套RecyclerView滑动卡顿问题简洁解决方案
其实仅仅需要给RecyclerView加一行控制代码即可:
mRecyclerView.setNestedScrollingEnabled(false);
这是最简洁的解决方案
过去网上也给出了其他的解决方法,比如是这样:
mLinearLayoutManager.setSmoothScrollbarEnabled(true);
mRecyclerView.setNestedScrollingEnabled(false);
但是实际上如果开发者再次给RecyclerView的布局控制器mLinearLayoutManager设置setSmoothScrollbarEnabled为true是多余的,因为在LinearLayoutManager的内部源代码中,默认的setSmoothScrollbarEnabled就是true,看LinearLayoutManager内部实现的源代码关键片段:
/**
* Works the same way as {@link android.widget.AbsListView#setSmoothScrollbarEnabled(boolean)}.
* see {@link android.widget.AbsListView#setSmoothScrollbarEnabled(boolean)}
*/
private boolean mSmoothScrollbarEnabled = true;
/**
* 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.
*
* When smooth scrollbar is disabled, the position and size of the scrollbar thumb is based
* solely on the number of items in the adapter and the position of the visible items inside
* the adapter. This provides a stable scrollbar as the user navigates through a list of items
* with varying widths / heights.
*
* @param enabled Whether or not to enable smooth scrollbar.
*
* @see #setSmoothScrollbarEnabled(boolean)
*/
public void setSmoothScrollbarEnabled(boolean enabled) {
mSmoothScrollbarEnabled = enabled;
}
即便不通过setSmoothScrollbarEnabled设置,mSmoothScrollbarEnabled已经为true了。因此在上层中再次setSmoothScrollbarEnabled(true)是多余的,多此一举。