Android ScrollView里嵌套RecyclerView時,在RecyclerView上滑動時出現卡頓(沖突)的現象


最近在項目中遇到一個現象,一個界面有一個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();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM