最近在項目中遇到一個現象,一個界面有一個RecyclerView(GridView型的),外面套了一層ScrollView,通過ScrollView上下滾動,但是在滑動的時候如果是在RecyclerView的內容上滑動,這時會出現滑動卡頓,而如果是在其他內容上滑動時就可以很順暢的滑下去。
這是RecyclerView和ScrollView的滑動事件沖突引起的,解決辦法就是禁止RecyclerView的滑動事件.
解決辦法:
方法一:
inearLayoutManager = new LinearLayoutManager(context) { @Override public boolean canScrollVertically() { return false; } };
方法二:
重寫LayoutManager或者GridLayoutManager
public class ScrollGridLayoutManager extends GridLayoutManager { private boolean isScrollEnabled = true; public ScrollGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public ScrollGridLayoutManager(Context context, int spanCount) { super(context, spanCount); } public ScrollGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) { super(context, spanCount, orientation, reverseLayout); } public void setScrollEnabled(boolean flag) { this.isScrollEnabled = flag; } @Override public boolean canScrollVertically() { //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll return isScrollEnabled && super.canScrollVertically(); } }