項目中時常須要實如今ScrollView中嵌入一個或多個RecyclerView。這一作法一般會致使以下幾個問題html
本文將利用多種方式分別解決上述問題java
若只存在滑動卡頓這一問題,能夠採用以下兩種簡單方式快速解決ide
recyclerView.setHasFixedSize(true); recyclerView.setNestedScrollingEnabled(false);
其中,setHasFixedSize(true)方法使得RecyclerView可以固定自身size不受adapter變化的影響;而setNestedScrollingeEnabled(false)方法則是進一步調用了RecyclerView內部NestedScrollingChildHelper對象的setNestedScrollingeEnabled(false)方法,以下佈局
public void setNestedScrollingEnabled(boolean enabled) { getScrollingChildHelper().setNestedScrollingEnabled(enabled); }
進而,NestedScrollingChildHelper對象經過該方法關閉RecyclerView的嵌套滑動特性,以下this
public void setNestedScrollingEnabled(boolean enabled) { if (mIsNestedScrollingEnabled) { ViewCompat.stopNestedScroll(mView); } mIsNestedScrollingEnabled = enabled; }
如此一來,限制了RecyclerView自身的滑動,整個頁面滑動僅依靠ScrollView實現,便可解決滑動卡頓的問題spa
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) { @Override public boolean canScrollVertically() { return false; } };
這一方式使得RecyclerView的垂直滑動始終返回false,其目的一樣是爲了限制自身的滑動code
如果須要綜合解決上述三個問題,則能夠採用以下幾種方式htm
在原有佈局中插入一層LinearLayout/RelativeLayout,造成以下佈局對象
該方法的核心思想在於經過重寫LayoutManager中的onMeasure()方法,即blog
@Override public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { super.onMeasure(recycler, state, widthSpec, heightSpec); }
從新實現RecyclerView高度的計算,使得其可以在ScrollView中表現出正確的高度,具體重寫方式可參考這篇文章
http://www.cnblogs.com/tianzh...
該方法的核心思想在於經過重寫ScrollView的onInterceptTouchEvent(MotionEvent ev)方法,攔截滑動事件,使得滑動事件可以直接傳遞給RecyclerView,具體重寫方式可參考以下
/** * Created by YH on 2017/10/10. */ public class RecyclerScrollView extends ScrollView { private int slop; private int touch; public RecyclerScrollView(Context context) { super(context); setSlop(context); } public RecyclerScrollView(Context context, AttributeSet attrs) { super(context, attrs); setSlop(context); } public RecyclerScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setSlop(context); } /** * 是否intercept當前的觸摸事件 * @param ev 觸摸事件 * @return true:調用onMotionEvent()方法,並完成滑動操做 */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: // 保存當前touch的縱座標值 touch = (int) ev.getRawY(); break; case MotionEvent.ACTION_MOVE: // 滑動距離大於slop值時,返回true if (Math.abs((int) ev.getRawY() - touch) > slop) return true; break; } return super.onInterceptTouchEvent(ev); } /** * 獲取相應context的touch slop值(即在用戶滑動以前,可以滑動的以像素爲單位的距離) * @param context ScrollView對應的context */ private void setSlop(Context context) { slop = ViewConfiguration.get(context).getScaledTouchSlop(); } }
事實上,儘管咱們可以採用多種方式解決ScrollView嵌套RecyclerView所產生的一系列問題,但因爲上述解決方式均會使得RecyclerView在頁面加載過程當中一次性顯示全部內容,所以當RecyclerView下的條目過多時,將會對影響整個應用的運行效率。基於此,在這種狀況下咱們應當儘可能避免採用ScrollView嵌套RecyclerView的佈局方式