[Android Pro] ScrollView嵌套RecyclerView時滑動出現的卡頓


reference to : http://zhanglu0574.blog.163.com/blog/static/113651073201641853532259/

ScrollView嵌套RecyclerView時滑動出現的卡頓 

現象:

一個界面有多個RecyclerView以及其他一些內容,這 時要上下滾動就會使用外面嵌套一個ScrollView,雖然我沒有遇到像ScrollView嵌套ListView時那樣只顯示部分,剩余不顯示,可能 是因為我內容少吧,所以沒有遇到這個,但是在滑動的時候如果是在RecyclerView上滑動,這時會出現只滑動動該RecyclerView的內容上 就會停止,而如果是在其他內容上滑動時就可以很順暢的滑下去,因此就會感覺到卡頓的樣子。

解決:禁止RecyclerView的滑動。
最簡單方便的就是直接
linearLayoutManager = new LinearLayoutManager(context) {
@Override
public boolean canScrollVertically() {
return false;
}
};
另外就是重寫LayoutManager,以Grid模式來說:
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